我对数据库查询不是很了解,但遇到错误
“如果SELECT DISTINCT是,则ORDER BY项目必须出现在选择列表中 指定。”
带有以下查询。我在Google上进行搜索,发现如果使用Distinct,则该订单按需要分组,但仍然无法获取。谁能帮助我。
SELECT DISTINCT
P1.*
FROM T_PRD P1
LEFT JOIN T_PRD P2 ON P1.baseprdid = P2.prdid
INNER JOIN T_PRD_NM_VENDOR prdNmVendor ON P1.prdId = prdNmVendor.prdId
INNER JOIN T_VENDOR_NM vendorNM ON prdNmVendor.vendorNMId = vendorNM.vendorNMId
INNER JOIN T_NM nm ON vendorNM.NMId = nm.NMId
INNER JOIN T_PRD_VENDOR prdVendor ON prdVendor.PRDId = P1.PRDId
INNER JOIN T_VENDOR vendor ON prdVendor.vendorId = vendor.vendorId
INNER JOIN T_CSTMR_PRD_REF custPrd ON custPrd.ProductId = P1.PRDId
INNER JOIN T_CSTMR cstmr ON custPrd.ChennelCstrId = cstmr.cstmrid
WHERE 1 = 1
AND P1.Lifecycle = 2
AND P1.AutoCreated = 0
AND vendor.vendorId = 1
AND P1.VendorEnfId = 1
AND cstmr.cstmrid = 2008
ORDER BY CASE
WHEN P1.BASEPRDID = 0
THEN P1.PRDNAME
ELSE P2.PRDNAME
END ASC,
BASEPRDID;
答案 0 :(得分:1)
您的ORDER BY取决于SELECT DISTINCT
列表中缺少的表达式。添加
SELECT DISTINCT P1.*
,CASE
WHEN P1.BASEPRDID = 0
THEN P1.PRDNAME
ELSE P2.PRDNAME
END col