javascript - 从两个表轮询数据 - heidisql
问题描述
我正在尝试从两个表中检索数据。以下代码工作正常:
const sql = "SELECT * FROM table_1, table_2 WHERE table_2.Id = table_1.tafId";
但我不想要 table_1 中的所有数据。我只想要名称为“某物”的数据。我尝试了以下代码,但它引发了异常:
const sql = "SELECT aa, bb, cc, dd FROM table_1 WHERE aa= '" + something + "', table_2 WHERE table_2.Id = table_1.tafId";
失败消息是:
check the manual that corresponds to your MariaDB server version for the right syntax to use near ' table_2 WHERE table_2.Id = table_1.tafId' at line 1",
sqlState: '42000',
index: 0,
我想念什么?
很高兴得到一些帮助。谢谢!
解决方案
您需要指定列属于哪个表。
或者你可以使用别名
假设aa
列属于table_1
和bb
列属于table_2
,这里是查询
const sql = "SELECT table_1.aa, table_2.bb FROM table_1, table_2 WHERE table_2.Id = table_1.tafId";
使用别名(别名可以是任何东西)。这里a1
和a2
是别名
const sql = "SELECT a1.aa, a2.bb FROM table_1 a1, table_2 a2 WHERE a1.Id = a2.tafId";
推荐阅读
- sql - 我需要执行一个 SQL 查询,从两个表中提取数据并对其进行排序
- javascript - Vue项目中如何正确实现axios拦截器
- tr - Pyside qwebenginWidget 没有打开 QWebenginProsser
- javascript - 相对于另一个元素放置元素
- android - 如何修复颤动中的垂直视点错误?
- c# - 使用 Jenkins 构建 Unity 时无法将项目路径设置为:C:\Program
- python - Python3 zipfile writestr,用户警告:名称重复
- google-chrome-extension - 选择一个子项时打开多个 URL
- python - 如何在 Python 中使用 Appsflyer API 获取指标
- android - 如何使用光标和循环显示来自sqlite的片段的recyclerview