gpt4 book ai didi

mysql - 如何确定记录的顺序作为连接查询的结果

转载 作者:搜寻专家 更新时间:2023-10-30 20:09:37 24 4
gpt4 key购买 nike

表格(来自 http://www.tutorialspoint.com/sql/sql-using-joins.htm )1)客户表:

 +----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+

2) 订单表:

+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
| 103 | 2008-05-20 00:00:00 | 4 | 2060 |
+-----+---------------------+-------------+--------+

内部连接查询是:

SELECT ID, NAME, AGE, AMOUNT
FROM CUSTOMERS, ORDERS
WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_ID;

显示的结果是:

+----+----------+-----+--------+
| ID | NAME | AGE | AMOUNT |
+----+----------+-----+--------+
| 3 | kaushik | 23 | 3000 |
| 3 | kaushik | 23 | 1500 |
| 2 | Khilan | 25 | 1560 |
| 4 | Chaitali | 25 | 2060 |
+----+----------+-----+--------+

我的问题:结果表显然是根据“ORDERS”表排序的,即结果表中 ID 列的排序是 3,3,2,4 acc。到订单表。为什么 ID 的排序不是根据 CUSTOMERS 表,即为什么结果不是

ID
2
3
3
4

和相应的值。是否有某种规则?

最佳答案

你应该像下面这样使用 order by

SELECT ID, NAME, AGE, AMOUNT
FROM CUSTOMERS, ORDERS
WHERE CUSTOMERS.ID = ORDERS.CUSTOMER_ID order by ID asc;

关于mysql - 如何确定记录的顺序作为连接查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25011306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com