简体   繁体   中英

What is the effect of this order_by clause?

I don't understand what this order_by clause is doing and whether I need it or not:

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;

This produces this data:

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

While if I remove the order_by clause completely, as in this query:

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;

I get these results:

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

I'm not sure what the order_by is doing here and if it's having the intended effects.

It looks like it is ordering on i.ordered_date , but using string comparison rather than date comparison, which is why 30-Jun-1999 is placed before 29-Feb-2000. As a string "30-Jun-1999" > "28-Feb-2000", but as dates, the reverse is true.

Check the type of i.ordered_date in the items_ordered table - it should be datetime or similar - if it's varchar, then you will need to either change it to a date type, or cast the value to a date in the order-by clause. Eg

   order by CAST(i.order_date AS DATE) desc

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM