如何通过访问多个表中的信息以获得最终结果来解决 sql 问题
how to solve sql problem by accessing information from multiple tables to get end result
你好,我这周正在写一些关于家庭作业的问题,但我被这个问题困住了。
我需要在不使用 join
的情况下从多个 table 收集信息
- 显示每个客户库存请求中请求的每种产品的列表。展示
客户名称、产品编号和请求数量,按客户名称排序,然后
产品编号。
我当前的代码显示了一个输出,但没有给出正确的结果。它打印 table 的内容 5 次,而不是根据问题打印一次。
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 , REQUEST_LIST.productNum
这里是 table:
(stock request)
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2020-12-10',1);
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2020-05-04',2);
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2021-07-06',3);
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2021-07-08',4);
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2022-02-07',5);
(request list)
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (1,1,1);
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (2,2,2);
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (3,3,3);
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (4,4,4);
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (5,5,5);
(client)
INSERT INTO Client (clientName)
VALUES ('Mike');
INSERT INTO Client (clientName)
VALUES ('John');
INSERT INTO Client (clientName)
VALUES ('Sally');
INSERT INTO Client (clientName)
VALUES ('Margret');
INSERT INTO Client (clientName)
VALUES ('Max');
如有任何帮助,我们将不胜感激。
预期输出:(不是实际数字)
client number | product number | quantity requested
John Smith. 1. 3
Margret Thatcher. 3. 7
Max Payne. 6. 1
Mike Costello. 2. 1
Sally Barbarella. 4. 4
首先,您不需要使用子查询从 stock_request
table 中获取 clien_num
和 request_num
值。只需在查询的 where
子句中使用条件即可。行集受 stock_request
条记录限制,行值允许从 client
和 request_list
table 中获取适当的值。
其次,我建议你不要使用区分大小写的 table 和列名,它可能会导致拼写错误。
最后,你的查询应该是这样的
SELECT
client.client_name,
request_list.product_num,
request_list.qty_requested
FROM client, request_list, stock_request
WHERE client.client_num = stock_request.client_num AND stock_request.request_num = request_list.request_num
ORDER BY client.client_name , request_list.product_num
勾选fiddlehttps://www.db-fiddle.com/f/nFQ3s1GGEELCMoPBvPYwyk/1
你好,我这周正在写一些关于家庭作业的问题,但我被这个问题困住了。 我需要在不使用 join
的情况下从多个 table 收集信息- 显示每个客户库存请求中请求的每种产品的列表。展示 客户名称、产品编号和请求数量,按客户名称排序,然后 产品编号。
我当前的代码显示了一个输出,但没有给出正确的结果。它打印 table 的内容 5 次,而不是根据问题打印一次。
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 , REQUEST_LIST.productNum
这里是 table:
(stock request)
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2020-12-10',1);
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2020-05-04',2);
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2021-07-06',3);
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2021-07-08',4);
INSERT INTO Stock_Request (requestDate, clientNum)
VALUES ('2022-02-07',5);
(request list)
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (1,1,1);
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (2,2,2);
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (3,3,3);
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (4,4,4);
INSERT INTO Request_List (requestNum, productNum, qtyRequested)
VALUES (5,5,5);
(client)
INSERT INTO Client (clientName)
VALUES ('Mike');
INSERT INTO Client (clientName)
VALUES ('John');
INSERT INTO Client (clientName)
VALUES ('Sally');
INSERT INTO Client (clientName)
VALUES ('Margret');
INSERT INTO Client (clientName)
VALUES ('Max');
如有任何帮助,我们将不胜感激。
预期输出:(不是实际数字)
client number | product number | quantity requested
John Smith. 1. 3
Margret Thatcher. 3. 7
Max Payne. 6. 1
Mike Costello. 2. 1
Sally Barbarella. 4. 4
首先,您不需要使用子查询从 stock_request
table 中获取 clien_num
和 request_num
值。只需在查询的 where
子句中使用条件即可。行集受 stock_request
条记录限制,行值允许从 client
和 request_list
table 中获取适当的值。
其次,我建议你不要使用区分大小写的 table 和列名,它可能会导致拼写错误。
最后,你的查询应该是这样的
SELECT
client.client_name,
request_list.product_num,
request_list.qty_requested
FROM client, request_list, stock_request
WHERE client.client_num = stock_request.client_num AND stock_request.request_num = request_list.request_num
ORDER BY client.client_name , request_list.product_num
勾选fiddlehttps://www.db-fiddle.com/f/nFQ3s1GGEELCMoPBvPYwyk/1