Reputation: 11
I am working on e-dictionary and i have 2 tables:
First table: Term
+----+--------------+ | id | term_text | +----+--------------+ | 1 | hello | | 2 | how are you? | | 3 | hola | +----+--------------+
Second table: Dictionary
<pre>
+-----------+------------+
| term_id_1 | term_id_2 |
+-----------+------------+
| 1 | 3 | /* hello - hola
| 2 | NULL | /* how are you? - NULL
+-----------+------------+
</pre>
I want to display like this:
<pre>
+--------------+-------------+
| term_text_1 | term_text_2 |
+--------------+-------------+
| hello | hola |
| how are you? | NULL |
+--------------+-------------+
</pre>
I was trying to get data using following query:
<pre>
SELECT t1.term_text_1, t2.term_text_2
FROM dictionary d, term t1, term t2
WHERE d.term_id_1 = t1.id
AND d.term_id_2 = t2.id
</pre>
But i was getting only where value is not null!
Upvotes: 1
Views: 38
Reputation: 164089
You need to join dictionary
with term
twice:
select
t1.term_text term_text_1,
t2.term_text term_text_2
from dictionary
left join term t1 on t1.id = term_id_1
left join term t2 on t2.id = term_id_2
See the demo.
Results:
| term_text_1 | term_text_2 |
| ------------ | ----------- |
| hello | hola |
| how are you? | |
Upvotes: 1
Reputation: 65218
You can use LEFT JOIN
SELECT t1.term_text as term_text_1, t2.term_text as term_text_2
FROM dictionary d
LEFT JOIN term t1 ON d.term_id_1 = t1.id
LEFT JOIN term t2 ON d.term_id_2 = t2.id
Upvotes: 1