我有此sql表,我需要从所有库存项目中获取表中的最新日期:在该日期支付价格的订单
table:orders
id
date
table:order_items
order_id
inventory_id
price
table:inventory
item_number
如果我做这样的事情:
SELECT inventory.item_number, orders.date, order_items.price
FROM inventory
INNER JOIN order_items ON inventory.id = order_items.inventory_id
INNER JOIN orders ON order_items.order_id = orders.id
WHERE max(orders.date)
它不起作用,并且出现错误。 正确的方法是什么
答案 0 :(得分:2)
您需要一个where
子句,但是MAX()
不能直接去那里。尝试子查询:
SELECT i.item_number, o.date, orders.price
FROM inventory i INNER JOIN
order_items oi
ON i.id = oi.inventory_id INNER JOIN
orders o
ON oi.order_id = o.id
WHERE o.date = (SELECt max(o2.date) FROM orders o2);