我在Laravel中通过原始SQL查询生成以下表格:
+----------+-------+-------+
| Products | Price | Shop |
+----------+-------+-------+
| Jacket | 300.0 | Shop1 |
| T-Shirt | 300.0 | Shop1 |
| Jacket | 500.0 | Shop2 |
| Shoes | 200.0 | Shop2 |
+----------+-------+-------+
不知道如何遍历这些数据,所以我在浏览器中得到这样的结果:
+---------+-------+-------+
| Product | Shop1 | Shop2 |
+---------+-------+-------+
| Jacket | 300.0 | 500.0 |
| T-Shirt | 300.0 | NULL |
| Shoes | NULL | 200.0 |
+---------+-------+-------+
答案 0 :(得分:1)
尝试使用
SELECT a.product,
(SELECT max(b.price)
FROM yourtable b
WHERE b.product = a.product
AND b.shop = 'Shop1') shop1,
(SELECT max(c.price)
FROM yourtable c
WHERE c.product = a.product
AND b.shop = 'Shop2') shop2
FROM yourtable a group by a.product