0

我正在尝试返回一个secret-identifier只有 myspace 和 facebook 作为应用程序的表格,而不是 myspace 或 facebook,但目前我只能获得第二个选项。到目前为止,这是我的代码

SELECT `secret-identifier`
FROM MyFavs__APP
WHERE app IN (
  SELECT DISTINCT app
  FROM MyFavs__APP
  WHERE `secret-identifier`='aha'
) 

我得到的结果是一个巨大的秘密标识符列表(有facebook或myspace),其中包括实际上有两个应用程序的4个。我如何摆脱只使用其中一个应用程序的所有人?

4

1 回答 1

2

you need to join the table to itself for both the myspace and facebook options:

create table  MyFavs__APP (
    secret_identifier int, 
    app varchar(16)
);

insert into MyFavs__APP (secret_identifier, app) values
    (1, 'facebook'),
    (1, 'myspace'),
    (2, 'facebook'),
    (3, 'myspace'),
    (4, 'myspace'),
    (4, 'facebook');

select distinct favs.secret_identifier
from   MyFavs__APP favs,
       MyFavs__APP m,
       MyFavs__APP f
where favs.secret_identifier = m.secret_identifier
and   favs.secret_identifier = f.secret_identifier
and   m.app = 'myspace' 
and   f.app = 'facebook';

+-------------------+
| secret_identifier |
+-------------------+
|                 1 |
|                 4 |
+-------------------+
2 rows in set (0.00 sec)
于 2013-06-14T05:39:00.580 回答