1
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。

4

4 回答 4

1

You can use JOIN in your query:

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
于 2012-04-30T09:34:44.930 回答
1
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)
;
于 2012-04-30T09:35:59.750 回答
0

Unfortunately INTERSECT doesnt work with MySQL. You could do this instead..

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
于 2012-04-30T09:32:52.543 回答
0

嗨,您可以使用 group by 和有声明

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

谢谢

于 2012-04-30T09:50:55.360 回答