Select columns from two tables based on 2 conditions

anna-banana's profile image anna-banana posted 4 years ago in Running SQL scripts Permalink

Hi, thank you for your great work :)

I am new to Heidi and SQL and I have been having some query issues. I have two tables in Heidi SQL, t1 and t2. I am trying to select the first 15 rows from the two tables 2 columns, column1 (t1) and column2 (t2) when two conditions are met: id1 (t1) = id1 (t2) and id2 (t1) = id2 (t2).

I tried:

SELECT a.column1, a.id1, a.id2, b.column2, b.id1, b.id2

FROM t1 a, t2 b LIMIT 15;

WHERE a.id1 = b.id1 AND a.id2 = b.id2

The selected rows do not follow the condition and I get the following error:

Error: SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL Server version for the right syntax to use near "WHERE a.id1 = b.id1 AND a.id2 = b.id2"

Any ideas?

ansgar's profile image ansgar posted 4 years ago Permalink

You have to move the whole WHERE clause between the FROM clause and the LIMIT clause:

SELECT a.column1, a.id1, a.id2, b.column2, b.id1, b.id2
FROM t1 a, t2 b
WHERE a.id1 = b.id1 AND a.id2 = b.id2
LIMIT 15;

Please login to leave a reply, or register at first.