我有一个相当简单的查询但想要根据某些情况生成的分数列来排序结果,我甚至不确定你是否可以在MYSQL中做这类事情。
但基本的想法是,未设置为“任意”的列越多,得分越高,它将位于列表的顶部。
数据示例如下:
Giant,Bikes,Road bike,Any,Any,得分高于 任何,自行车,公路自行车,任何,任何
理想情况下,我希望它能够发挥作用(如果可能的话):
SELECT Brand, Cat1, Cat2, Cat3, Gender,
CASE
WHEN Brand ='Any' THEN score = 0 ELSE score = 1
WHEN Cat1 !='Any' THEN score +1 ELSE score = score (stays the same)
WHEN Cat2 !='Any' THEN score +1 ELSE score = score (stays the same)
and so on....
END as weight
FROM products_sizeguides
ORDER BY weight DESC
由于
答案 0 :(得分:1)
尝试一起添加而不是尝试添加:
SELECT Brand, Cat1, Cat2, Cat3, Gender,
IF(Brand = 'Any',0,1)
+ IF(Cat1 !='Any,1,0)
+ IF(Cat2 !='Any,1,0)
as weight
FROM products_sizeguides
ORDER BY weight DESC
答案 1 :(得分:1)
您可以使用添加
select
...
case when Brand = 'Any' Then 0 else 1 end
+ case when Cat1 = 'Any' then 0 else 1 end
+ case when Cat2 = 'Any' then 0 else 1 end
as weight
...
order by weight