有人可以帮助我,
这是我的数据库sql查询 My Table
SELECT supplier,
MAX(IF(item_name = 'Lemon', price, NULL)) AS Lemon,
MAX(IF(item_name = 'Apple', price, NULL)) AS Apple,
MAX(IF(item_name = 'Durian', price, NULL)) AS Durian,
MAX(IF(item_name = 'Mango', price, NULL)) AS Mango
FROM offer
where status = 'open'
GROUP BY supplier
Laravel QueryBuilder如何使用此查询?感谢的......
答案 0 :(得分:0)
$results = DB::table('offer')->select(DB::raw("supplier,
MAX(IF(item_name = 'Lemon', price, NULL)) AS Lemon,
MAX(IF(item_name = 'Apple', price, NULL)) AS Apple,
MAX(IF(item_name = 'Durian', price, NULL)) AS Durian,
MAX(IF(item_name = 'Mango', price, NULL)) AS Mango"))
->where('status', 'open')
->groupBy('supplier')
->get();
实际上,查询生成器并不适用于复杂的查询。当它们变得复杂时,您可能会发现使用原始语句更容易:
$results = DB::select("SELECT supplier,
MAX(IF(item_name = 'Lemon', price, NULL)) AS Lemon,
MAX(IF(item_name = 'Apple', price, NULL)) AS Apple,
MAX(IF(item_name = 'Durian', price, NULL)) AS Durian,
MAX(IF(item_name = 'Mango', price, NULL)) AS Mango
FROM offer
where status = 'open'
GROUP BY supplier");