SQL查询pubs数据库订单上有多个标题的商店

时间:2016-09-26 20:54:11

标签: sql database pubs

pubs数据库中,我想查找订单中包含多个标题的订单的商店。我已经加入了表格,但申请的逻辑用于查找具有多个标题但我无法理解的订单

SELECT 
    title, stores.stor_name, S.ord_num, S.qty
FROM 
    sales S
JOIN
    titles T ON (S.title_id = T.title_id)
JOIN
    stores ON (S.stor_id = stores.stor_id)

enter image description here

1 个答案:

答案 0 :(得分:0)

类似

    SELECT 
    title, stores.stor_name, S.ord_num, S.qty
FROM 
    sales S
JOIN
    titles T ON (S.title_id = T.title_id)
JOIN
    stores ON (S.stor_id = stores.stor_id)
where S.ord_num in (

    SELECT 
    S1.ord_num
FROM 
    sales S1
JOIN
    titles T1 ON (S1.title_id = T1.title_id)
JOIN
    stores  st ON (S1.stor_id = st.stor_id)
    group by S1.ord_num
    having count( distinct title)>1
    )