Search code examples
selectconditional-statementsheidisql

HeidiSQL Select columns from two tables based on two conditions


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?


Solution

  • You have to move the 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;