Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

php - Tough Mysql Query

I got a skill test for a quick mysql query. I am given the tables:

    Orders                    OrderItems
----------------------------------------                
    id                         id
    date                       order_id(Orders.id)
    shipping_amount            product_id
    order_status               price
    customer_id             quantity

I need to show Orders id and totals (including shipping amount) and records prior to June 1, 2003.

output should be...

| OrderID   | OrderTotal |
+-----------+------------+
| 13230     | $55.00     |
| 54455     | $40.00     |
| 59694     | $33.04     |
| 39495     | $21.05     |

The hint is to use Group by or subselect. I have the following statement so far but not sure what to do next.

Select id AS OrderId,***(not sure what to do here) 
from Orders join OrderItems on Orders.id=OrderItems.id 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't have access to a mysql database to test, but I would imagine it looks something like this:

select
  o.id OrderID,
 (select sum(oi.price * oi.quantity) from order_items oi where oi.order_id = o.id) + o.shipping_amount OrderTotal
from
  orders o
where
  o.date < str_to_date('2003-06-01', '%Y-%m-%d');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...