我想在我编写的联接中使用order by子句,但是它显示了一个错误,我无法解决此问题:
SELECT
products_details.*, master_category.title as cat1
FROM
products_details
JOIN
master_category ON products_details.product_category = master_category.id
ORDER BY
products_details.product_name
WHERE
1 = 1
AND products_details.status != 2;
有人可以帮忙吗?
答案 0 :(得分:1)
WHERE
在ORDER BY
之前:
SELECT
pd.*,
mc.title AS cat1
FROM products_details pd
INNER JOIN master_category mc
ON pd.product_category = mc.id
WHERE
pd.status != 2
ORDER BY
pd.product_name;
此外,我在查询表的过程中引入了别名,使其更易于阅读。
答案 1 :(得分:0)
您的order by子句必须在where条件之后。 如下所示:-
SELECT
products_details.*, master_category.title as cat1
FROM
products_details
JOIN
master_category ON products_details.product_category = master_category.id
WHERE
1 = 1 AND products_details.status != 2
ORDER BY
products_details.product_name;