SELECT STATNO FROM ENQUIRY WHERE ID=12;
返品:100,102,110
SELECT STATNO FROM ENQUIRY WHERE ID=16;
返品:102,112,118
SELECT STATNO FROM ENQUIRY WHERE ID=21;
返品:101,102,120
上記を組み合わせて共通データを返すクエリが必要です:102。
クエリでJOINを使用できます。
SELECT a.STATNO FROM ENQUIRY a
JOIN (
SELECT x.STATNO FROM ENQUIRY x WHERE ID=16
) b ON a.STATNO = b.STATNO
JOIN (
SELECT y.STATNO FROM ENQUIRY y WHERE ID=21
) c ON b.STATNO = c.STATNO
WHERE a.ID=12
drop table if exists enquiry;
create table enquiry (id integer, statno integer);
insert into enquiry values
(12,100),(12,102),(12,110),
(18,102),(18,112),(18,118),
(21,101),(21,102),(21,120);
select distinct statno from enquiry where
statno in (select statno from enquiry where id=12)
and statno in (select statno from enquiry where id=18)
and statno in (select statno from enquiry where id=21)
;
こんにちはあなたはステートメントを持っていることでgroupbyを使うことができます
SELECT STATNO FROM ENQUIRY WHERE ID IN (12 ,16 , 21) group by STATNO having count(*) = 3;
このアプローチは、がの場合にのみ機能しcombination of (ID, STATNO) is unique
ます。ここcount(*) would be equal to total number of values in IN statement in query like here is 3
に。
ありがとう
残念ながらINTERSECT
、MySQLでは動作しません。代わりにこれを行うことができます。
SELECT a.STATNO
FROM ENQUIRY AS a
INNER JOIN ENQUIRY AS b ON b.STATNO = a.STATNO
INNER JOIN ENQUIRY AS c on c.STATNO = a.STATNO
WHERE a.id = 12 and b.id = 16 and c.id =21