我有两个表订单和order_items
订单表
+----+-----------------------+ | id | total_shipping_charge | +----+-----------------------+ | 1 | 60 | | 2 | 60 | | 3 | 60 | | 4 | 60 | | 5 | 60 | | 6 | 0 | | 7 | 60 | | 8 | 0 | | 9 | 60 | | 10 | 60 | +----+-----------------------+
order_items表
+----+--------------------------+-----+-------+- | id | order_id | p_name | qty | price | +----+----------+---------------+-----+-------+--------------------------------- | 1 | 1 | Product 1 | 1 | 699 | | 2 | 2 | Product 2 | 1 | 349 | | 3 | 3 | Product 3 | 1 | 199 | | 4 | 4 | Product x | 2 | 299 | | 5 | 4 | Product y | 2 | 299 | | 6 | 4 | Product z | 2 | 299 | | 7 | 5 | Product 7 | 2 | 299 | | 8 | 6 | Product 8 | 1 | 1299 | | 9 | 7 | Product 9 | 1 | 199 | | 10 | 8 | Product 10 | 6 | 349 | +----+----------+---------------------------------------------------------------
现在order_id 4在一个订单中有多个产品,当我加入两个表时,我得到重复的运费。我只想在第一次生吃时运费。
我运行了这个查询
SELECT
o1.id,
o1.total_shipping_charge,
o2.order_id,
o2.p_name
FROM
orders o1
INNER JOIN order_items o2 ON o1.id = o2.order_id
但是它显示了运费的重复行,它应该只显示第一行。
| id | total_shipping_charge | order_id | p_name +----+-----------------------+----------+---------+ | 1 | 60 | 1 | Product 1 | 2 | 60 | 2 | Product 2 | 3 | 60 | 3 | Product 3 | 4 | 60 | 4 | Product x | 5 | 60 | 4 | Product y | 6 | 60 | 4 | Product z | 7 | 0 | 6 | Product 7 | 8 | 60 | 7 | Product 8 | 9 | 0 | 8 | Product 9
答案 0 :(得分:1)
从Sql Server的角度来看,您要求的是不可能的。您从报告的角度询问哪些是不同的。您可以提取此数据,并在报告中以您希望的方式显示它,但您无法以这种方式提取数据。
不确定MySql的行为。
答案 1 :(得分:0)
尝试
SELECT o1.id,o1.total_shipping_charge, distinct o2.order_id,o2.p_name FROM orders o1 INNER JOIN order_items o2 ON o1.id=o2.order_id