mysql – 在多个表中为同一个键选择多个值
作者:互联网
我在以下结构中有两个表
表格1
uid | name | age
1 | John | 24
2 | Adam | 35
3 | Sara | 26
TABLE_2
id | uid | meta_key | meta_value
1 | 2 | location | NY
2 | 2 | school | NYU
3 | 3 | Location | NY
4 | 3 | school | XYZ
6 | 1 | location | NY
6 | 1 | school | NYU
我想要做的是从table_1选择他们的位置是纽约,学校是纽约大学的用户
这是我尝试使用的查询没有运气
SELECT
tabl_1.uid
FROM `tabl_1`, `tabl_2`
WHERE
tabl_1.uid = tabl_2.uid
AND table_2.meta_key IN ('location', 'school')
AND table_2.meta_value IN ('NY', 'NYU')
ORDER BY tabl_1.uid ASC
如果您有一个可行的查询或者一个非常感谢的解决方案的链接,我已经无处运气地到处寻找,谢谢.
解决方法:
你应该试试
SELECT t1.uid
FROM tabl_1 t1 INNER JOIN tabl_2 t2
ON t1.uid = t2.uid AND t2.meta_key = 'location' AND t2.meta_value = 'NY'
INNER JOIN tabl_2 t3
ON t1.uid = t3.uid AND t3.meta_key = 'school' AND t3.meta_value = 'NYU'
检查结果http://sqlfiddle.com/#!2/f35ef/1/0
标签:multiple-tables,mysql 来源: https://codeday.me/bug/20190831/1776820.html