我必须按价格订购20种最新产品。 我正在使用此查询。
SELECT * FROM products ORDER BY created_at DESC, price ASC LIMIT 20
答案 0 :(得分:0)
你可以尝试自我加入
SELECT p1.* FROM products AS p1
INNER JOIN
(SELECT id
FROM products
ORDER BY created_at DESC LIMIT 20) AS p2
ON p1.id = p2.id
ORDER BY p1.price ASC
我不确定是否要加入laravel query-builder中的临时表。 至少你可以这样做:
$results = DB::select(DB::raw("
SELECT p1.* FROM products AS p1
INNER JOIN
(SELECT id
FROM products
ORDER BY created_at DESC LIMIT 20) AS p2
ON p1.id = p2.id
ORDER BY p1.price ASC
"));