[英]How to use PHP to query MySQL for certain rows, then for each row, based on a UID, query another table based on value?
[英]How to query based on values based on multiple rows in other table associated with row
例如我有這些表:
A:
id | something etc. ---|-------------- 1 | bla 2 | die 3 | bla
B:
a_id | key | value -----|-----| 1 | x | a 1 | y | b 1 | z | c 2 | x | a 2 | z | d 3 | x | a 3 | z | c
我如何進行查詢,從表A中獲取所有具有ID的行,該ID在表b中鏈接,並且具有鍵x和值a以及鍵z和值c,因此在此示例中應導致行1和3。
我真的無法解決這個問題,我們將不勝感激。
select a.*
from a
join
(
select a_id
from b
group by a_id
having sum(key = 'x' and value = 'a') > 0
and sum(key = 'z' and value = 'c') > 0
) b_tmp on b_tmp.a_id = a.id
子查詢從表b
獲取所有相關記錄。 再加入一次,您就會有結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.