tbl_picklist table
id value
1 John
2 Mumbai
3 San Diego
4 CA
5 Jerry
tbl_user table
id picklist_id_name email
1 1 [email protected]
2 8 [email protected]
tbl_profile table
id user_id picklist_id_addr picklist_id_addr2 designation
1 1 3 5 Dev
3 2 7 3 QA
Picklist is table where you will get values of address, address2 and name, I wrote query which give me desired result
SELECT
u.email,
p1.value AS username,
p2.value AS addr1,
p3.value AS addr2
FROM tbl_user u
LEFT JOIN tbl_picklist AS p1
ON u.picklist_id_name = p1.id
LEFT JOIN tbl_profile pr
ON pr.user_id = u.id
LEFT JOIN tbl_picklist AS p2
ON p2.id = pr.picklist_id_addr
LEFT JOIN tbl_picklist AS p3
ON p3.id = pr.picklist_id_addr2
Result:
email username addr1 addr2
[email protected] John Mumbai San Diego
[email protected] Jerry CA Mumbai
To get above result, i needed to write 3 joins on picklist table, is there any other way in which i can get result only with the one join on picklist table? I have this kind of table structure in application, many values are stored in picklist table, its taking to much time to execute this kind of queries.
explain analyzeand provide the exact index definitions that are applied to those tables.