这个order_by子句有什么影响?

时间:2010-06-09 08:07:05

标签: sql

我不明白这个order_by子句在做什么以及我是否需要它:

select c.customerid, c.firstname, c.lastname, i.order_date, i.item, i.price from 
items_ordered i, customers c 
where i.customerid = c.customerid
group by c.customerid, i.item, i.order_date
order by i.order_date desc;

这会产生以下数据:

10330   Shawn   Dalton  30-Jun-1999 Pogo stick  28.00
10101   John    Gray    30-Jun-1999 Raft    58.00
10410   Mary Ann    Howell  30-Jan-2000 Unicycle    192.50
10101   John    Gray    30-Dec-1999 Hoola Hoop  14.75
10449   Isabela Moore   29-Feb-2000 Flashlight  4.50
10410   Mary Ann    Howell  28-Oct-1999 Sleeping Bag    89.22
10339   Anthony Sanchez 27-Jul-1999 Umbrella    4.50
10449   Isabela Moore   22-Dec-1999 Canoe   280.00
10298   Leroy   Brown   19-Sep-1999 Lantern 29.00
10449   Isabela Moore   19-Mar-2000 Canoe paddle    40.00
10413   Donald  Davids  19-Jan-2000 Lawnchair   32.00
10330   Shawn   Dalton  19-Apr-2000 Shovel  16.75
10439   Conrad  Giles   18-Sep-1999 Tent    88.00
10298   Leroy   Brown   18-Mar-2000 Pocket Knife    22.38
10299   Elroy   Keller  18-Jan-2000 Inflatable Mattress 38.00
10438   Kevin   Smith   18-Jan-2000 Tent    79.99
10101   John    Gray    18-Aug-1999 Rain Coat   18.30
10449   Isabela Moore   15-Dec-1999 Bicycle 380.50
10439   Conrad  Giles   14-Aug-1999 Ski Poles   25.50
10449   Isabela Moore   13-Aug-1999 Unicycle    180.79
10101   John    Gray    08-Mar-2000 Sleeping Bag    88.70
10299   Elroy   Keller  06-Jul-1999 Parachute   1250.00
10438   Kevin   Smith   02-Nov-1999 Pillow  8.50
10101   John    Gray    02-Jan-2000 Lantern 16.00
10315   Lisa    Jones   02-Feb-2000 Compass 8.00
10449   Isabela Moore   01-Sep-1999 Snow Shoes  45.00
10438   Kevin   Smith   01-Nov-1999 Umbrella    6.75
10298   Leroy   Brown   01-Jul-1999 Skateboard  33.00
10101   John    Gray    01-Jul-1999 Life Vest   125.00
10330   Shawn   Dalton  01-Jan-2000 Flashlight  28.00
10298   Leroy   Brown   01-Dec-1999 Helmet  22.00
10298   Leroy   Brown   01-Apr-2000 Ear Muffs   12.50

如果我完全删除了order_by子句,就像在这个查询中一样:

select c.customerid, c.firstname, c.lastname, i.order_date, i.item, i.price from 
items_ordered i, customers c 
where i.customerid = c.customerid
group by c.customerid, i.item, i.order_date;

我得到了这些结果:

10101   John    Gray    30-Dec-1999 Hoola Hoop  14.75
10101   John    Gray    02-Jan-2000 Lantern 16.00
10101   John    Gray    01-Jul-1999 Life Vest   125.00
10101   John    Gray    30-Jun-1999 Raft    58.00
10101   John    Gray    18-Aug-1999 Rain Coat   18.30
10101   John    Gray    08-Mar-2000 Sleeping Bag    88.70
10298   Leroy   Brown   01-Apr-2000 Ear Muffs   12.50
10298   Leroy   Brown   01-Dec-1999 Helmet  22.00
10298   Leroy   Brown   19-Sep-1999 Lantern 29.00
10298   Leroy   Brown   18-Mar-2000 Pocket Knife    22.38
10298   Leroy   Brown   01-Jul-1999 Skateboard  33.00
10299   Elroy   Keller  18-Jan-2000 Inflatable Mattress 38.00
10299   Elroy   Keller  06-Jul-1999 Parachute   1250.00
10315   Lisa    Jones   02-Feb-2000 Compass 8.00
10330   Shawn   Dalton  01-Jan-2000 Flashlight  28.00
10330   Shawn   Dalton  30-Jun-1999 Pogo stick  28.00
10330   Shawn   Dalton  19-Apr-2000 Shovel  16.75
10339   Anthony Sanchez 27-Jul-1999 Umbrella    4.50
10410   Mary Ann    Howell  28-Oct-1999 Sleeping Bag    89.22
10410   Mary Ann    Howell  30-Jan-2000 Unicycle    192.50
10413   Donald  Davids  19-Jan-2000 Lawnchair   32.00
10438   Kevin   Smith   02-Nov-1999 Pillow  8.50
10438   Kevin   Smith   18-Jan-2000 Tent    79.99
10438   Kevin   Smith   01-Nov-1999 Umbrella    6.75
10439   Conrad  Giles   14-Aug-1999 Ski Poles   25.50
10439   Conrad  Giles   18-Sep-1999 Tent    88.00
10449   Isabela Moore   15-Dec-1999 Bicycle 380.50
10449   Isabela Moore   22-Dec-1999 Canoe   280.00
10449   Isabela Moore   19-Mar-2000 Canoe paddle    40.00
10449   Isabela Moore   29-Feb-2000 Flashlight  4.50
10449   Isabela Moore   01-Sep-1999 Snow Shoes  45.00
10449   Isabela Moore   13-Aug-1999 Unicycle    180.79

我不确定order_by在这里做了什么以及它是否具有预期效果。

2 个答案:

答案 0 :(得分:3)

看起来它是在i.ordered_date上订购,但是使用字符串比较而不是日期比较,这就是为什么1999年6月30日在2000年2月29日之前放置。作为字符串“30-Jun-1999”> “2000年2月28日”,但作为日期,情况恰恰相反。

检查i.ordered_date表中items_ordered的类型 - 它应该是datetime或类似的 - 如果它是varchar,那么你需要将其更改为日期类型,或者将值转换为order-by子句中的日期。 E.g。

   order by CAST(i.order_date AS DATE) desc

答案 1 :(得分:0)

您应始终使用正确的DATETIME数据类型来存储日期