我有一些像这样的MySQL表:
ids urns urn_urls urls
id | type id | uid | urn uid | lid lid | url
---+----- ---+-----+----- ----+---- ----+----
1 | 1 1 | 1 | urn1 1 | 1 1 | url1
2 | 4 2 | 2 | urn2 1 | 2 2 | url2
3 | 4 2 | 3 | urn3 2 | 3 3 | url3
4 | 4 3 | 4 | urn4 2 | 4 4 | url4
...|... 3 | 5 | urn5 3 | 4 5 | url5
. 4 | 6 | urn6 4 | 5 6 | url6
. ...| ... | ... 5 | 5 ...| ...
. 6 | 1
. 6 | 6
我想要的是一个查询,它列出了类型4的id条目,它们与多个urn和相应的urns相关,如下所示:
result
id | url
---+-----
2 | url3
2 | url4
4 | url1
4 | url6
我已经尝试过这样的事情:
SELECT id,url
FROM ids
NATURAL JOIN urns
NATURAL JOIN urn_urls
NATURAL JOIN urls
WHERE type=4
HAVING count( #column# )>1;
#column#
之一id
,lid
,url
),但它只返回行;
当我添加GROUP BY
语句时,我得到的更多,但
SELECT id,url
FROM ids
NATURAL JOIN urns
NATURAL JOIN urn_urls
NATURAL JOIN urls
WHERE type=4
GROUP BY #group#
HAVING count( #column# )>1;
表示#group#
/ #column#
,id
,lid
之内url
/ {{1}}的不同组合。
所以,请帮助。
答案 0 :(得分:0)
怎么样 -
SELECT id, GROUP_CONCAT(urls.url SEPARATOR ' ')
FROM ids
NATURAL JOIN urns
NATURAL JOIN urn_urls
NATURAL JOIN urls
WHERE type=4
GROUP BY ids.id
HAVING COUNT(DISTINCT urls.url)>1;