/* Create a table called NAMES */
CREATE TABLE test(Id integer PRIMARY KEY, group text, content text);
/* Create few records in this table */
INSERT INTO test VALUES(1,'mygroup','foobar');
INSERT INTO test VALUES(2,'myothergroup','foobar');
INSERT INTO test VALUES(3,'myothergroup','foobaz');
INSERT INTO test VALUES(4,'gr1','foobaz');
INSERT INTO test VALUES(5,'gr0','foobaz');
COMMIT;
我有一个像上面这样的SQL表。
我想找到所有内容,这些内容出现在我的所有小组中。
我的查询如下所示:
SELECT DISTINCT content from test WHERE group like 'my%' and content =
ALL(SELECT content from test WHERE group like 'my%');
似乎无效,因为它什么都不返回,它应该返回foobar,因为foobar存在于从我开始的所有可能的组中。
例如:2
让我们说我想找到以gr开头的所有群组中的所有内容:
SELECT DISTINCT content from test WHERE group like 'gr%' and content =
ALL(SELECT content from test WHERE group like 'gr%');
在这种情况下,它完全正常并且返回foobaz,因为foobaz存在于以gr开头的所有可能的组中。
请帮忙。
答案 0 :(得分:1)
你似乎想要:
select content
from test
where group like 'my%'
group by content
having count(distinct group) = (select count(distinct group) from test where group like 'my%');
答案 1 :(得分:0)
您只需要一个加入查询
SELECT DISTINCT t1.content from test t1 join test t2 on t1.Id=t2.Id and t1.group_g like 'my%'
SELECT DISTINCT t1.content from test t1 join test t2 on t1.Id=t2.Id and t1.group_g like 'gr%'