I am trying to retrieve data from two tables. Following code works fine:
const sql = "SELECT * FROM table_1, table_2 WHERE table_2.Id = table_1.tafId";
But I dont´t want all the data from table_1. I only want the data with the name "something". I tried following code, but it throws an exception:
const sql = "SELECT aa, bb, cc, dd FROM table_1 WHERE aa= '" + something + "', table_2 WHERE table_2.Id = table_1.tafId";
The failure message is:
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,
What do I miss?
Would be happy for some help. Thanks!
You need to specify column belongs to which table.
or you can make use of alias
Assuming aa
column belongs to table_1
and bb
column belongs to table_2
,
here is the query
const sql = "SELECT table_1.aa, table_2.bb FROM table_1, table_2 WHERE table_2.Id = table_1.tafId";
using alias(alias name can be anything). Here a1
and a2
are alias
const sql = "SELECT a1.aa, a2.bb FROM table_1 a1, table_2 a2 WHERE a1.Id = a2.tafId";