如何在html表中显示原始sql查询输出

时间:2016-09-08 17:48:33

标签: php mysql foreach

我在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 |
+---------+-------+-------+

1 个答案:

答案 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