I have MySql database with 3 tables:
language
lang_id lang
word
word_id lang_id word
translation
trans_id word_id1 word_id2
And I want to get all translations between two languages I choose. Following command (with a wrong syntax) is based on:
Two foreign keys of the same table. How do I write this SELECT statement?
MySQL multiple Inner Join with Where clause
In the command I'm trying to get translations between languages 1 and 2.
SELECT w1.word, lang1.lang AS w2.word, lang2.lang AS FULL
FROM translation AS trans
(JOIN (word AS w1 JOIN language AS lang1 ON lang1.id_lang=w1.lang_id)
ON trans.word_id1=w1.id_word WHERE (w1.lang_id=1 OR w1.lang_id=2))
(JOIN (word AS w2 JOIN language AS lang2 ON lang2.id_lang=w2.lang_id)
ON trans.word_id2=w2.id_word WHERE (w2.lang_id=1 OR w2.lang_id=2))
SOLUTION Thanks to @ahmed-gdoma I came up with this command:
SELECT w1.word AS w1, l1.lang AS L1,w2.word AS w2, l2.lang AS L2 FROM translation AS t
JOIN word AS w1 on w1.id_word=t.word_id1
JOIN word AS w2 on w2.id_word=t.word_id2
JOIN language AS L1 on w1.lang_id=L1.id_lang
JOIN language AS L2 on w2.lang_id=L2.id_lang
WHERE (l2.id_lang=1 OR l2.id_lang=2)
AND (l1.id_lang=2 OR l1.id_lang=1)
It returns word (w1), its language (L1), translation of the word (w2) and language of the translation (L2)