如何将此sql查询更改为子查询

时间:2014-06-06 22:28:45

标签: mysql sql

显示每个客户库存请求所请求的每个产品的列表。显示客户名称,产品编号和要求的数量。按客户名称排序,然后按产品编号排序。

我试过了:

SELECT STOCK_REQUEST.requestNum, STOCK_REQUEST.clientNum, CLIENT.clientName, REQUEST_LIST.productNum, REQUEST_LIST.qtyRequested 
FROM STOCK_REQUEST, CLIENT, REQUEST_LIST 
WHERE CLIENT.clientNum = STOCK_REQUEST.clientNum 
AND REQUEST_LIST.requestNum = STOCK_REQUEST.requestNum 
ORDER BY CLIENT.clientName AND REQUEST_LIST.productNum

我是这样的:

requestNum  clientNum   clientName  productNum  qtyRequested
2            2 [->]     David Liu       4            674
3            2 [->]     David Liu       5            66
1            1 [->]     Ian Peng        2            45
5            4 [->]     James Cameron   3            809
4            3 [->]     Mark Moris      1            164

但要求说我们必须使用子查询,所以我尝试将其更改为:

SELECT CLIENT.clientName, REQUEST_LIST.productNum, REQUEST_LIST.qtyRequested 
FROM CLIENT, REQUEST_LIST 
WHERE CLIENT.clientNum IN (
    SELECT clientNum
    FROM STOCK_REQUEST)
    AND REQUEST_LIST.requestNum IN (
        SELECT requestNum
        FROM STOCK_REQUEST)
ORDER BY CLIENT.clientName AND REQUEST_LIST.productNum

但结果不是我所期望的:

clientName  productNum  qtyRequested
David Liu       4           674
James Cameron   3           809
Mark Moris      5           66
David Liu       2           45
James Cameron   1           164
Mark Moris      4           674
James Cameron   5           66
Mark Moris      2           45
Ian Peng        3           809
James Cameron   4           674
Ian Peng        1           164
James Cameron   2           45
David Liu       3           809
Ian Peng        5           66
David Liu       1           164
Ian Peng        4           674
Mark Moris      3           809
David Liu       5           66
Ian Peng        2           45
Mark Moris      1           164

0 个答案:

没有答案