SELECT K.OBJECT_ID, K.`TYPE`
FROM KB_OBJECT_IDENTIFIERS K
WHERE K.`TYPE` IN ('ISSN', 'ISBN')
GROUP BY K.OBJECT_ID
HAVING count(K.OBJECT_ID) = 2;


在这样的桌子上

OBJECT_ID   TYPE
1              ISSN
1              ISBN
2              ISSN
2              ISSN


我需要显示两个对象的结果,因为它们同时具有ISSN和ISBN,但没有对象ID的行却是2,因为它们都是ISSN。

当前,我的查询将显示第一条记录(仅一次),还将显示第二条记录。

最佳答案

select * from KB_OBJECT_IDENTIFIERS
where OBJECT_ID in
(
    SELECT K.OBJECT_ID
    FROM KB_OBJECT_IDENTIFIERS K
    WHERE K.`TYPE` IN ('ISSN', 'ISBN')
    GROUP BY K.OBJECT_ID
    HAVING count(distinct K.OBJECT_ID) = 2
)

关于mysql - 修改此SQL语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42067767/

10-10 16:24