首页 > 解决方案 > 从两个表轮询数据 - 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,

我想念什么?

很高兴得到一些帮助。谢谢!

标签: javascriptheidisql

解决方案


您需要指定列属于哪个表。

或者你可以使用别名

假设aa列属于table_1bb列属于table_2,这里是查询

const sql = "SELECT table_1.aa, table_2.bb FROM table_1, table_2 WHERE table_2.Id = table_1.tafId";

使用别名(别名可以是任何东西)。这里a1a2是别名

const sql = "SELECT a1.aa, a2.bb FROM table_1 a1, table_2 a2 WHERE a1.Id = a2.tafId";

推荐阅读