Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

conditional statements - 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?

question from:https://stackoverflow.com/questions/66062714/heidisql-select-columns-from-two-tables-based-on-two-conditions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...