i have a table named 'language' as shown below
personid | lang
-------------------
1 | english
1 | french
1 | italian
2 | italian
3 | french
3 | italian
4 | japanese
I wish to select all personid's with any of the language that personid 1 know. (That is, any personid which have values english, french or italaian)
I have used the following query and got the solution. But it seems little bit slower. (I think it's due to 'IN' clause). Is there any other query option for faster execution.
SELECT distinct personid FROM language WHERE personid!=1 AND lang IN (SELECT lang FROM language WHERE personid=1)
You can do a self-join of the language table:
SELECT DISTINCT l2.personid AS personid FROM
language l1 INNER JOIN language l2
ON l1.lang = l2.lang
WHERE l1.personid = 1;
Output:
+----------+
| personid |
+----------+
| 2 |
| 3 |
+----------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With