使用点系统排序SQL结果

时间:2013-04-03 11:19:14

标签: mysql sql case

我正在尝试通过某个'点系统'来获取我的SQL结果。对于填写的每个字段,搜索结果都会获得一定数量的点。在我的SQL结束时,我添加了以下ORDER BY代码,但是,如果我将其中一个“1”数字更改为更高的数字,则不会更改搜索结果顺序。为什么那不起作用? (在此示例中将bedrijfslogo-case更改为9):

谢谢你们!

$sql .= " ORDER BY

case when ID is not null then 1 else 0 end +
case when username is not null then 1 else 0 end +
case when password is not null then 1 else 0 end +
case when voornaam is not null then 1 else 0 end +
case when achternaam is not null then 1 else 0 end +
case when telefoonnummerP is not null then 1 else 0 end +
case when emailP is not null then 1 else 0 end +
case when functie is not null then 1 else 0 end +
case when bedrijfsnaam is not null then 1 else 0 end +
case when bedrijfsslogan is not null then 1 else 0 end +
case when bedrijfslogo is not null then 9 else 0 end +
case when bedrijfsfoto is not null then 1 else 0 end +
case when bedrijfsfoto2 is not null then 1 else 0 end +
case when bedrijfsfoto3 is not null then 1 else 0 end +
case when bedrijfsfoto4 is not null then 1 else 0 end +
case when bedrijfsomschrijving is not null then 1 else 0 end +
case when bedrijfsspecialiteiten is not null then 1 else 0 end +
case when bedrijfsgeschiedenis is not null then 1 else 0 end +
case when openingstijden is not null then 1 else 0 end +
case when kvk is not null then 1 else 0 end + 
case when straatnaam is not null then 1 else 0 end + 
case when huisnummer is not null then 1 else 0 end + 
case when postcode is not null then 1 else 0 end + 
case when plaats is not null then 1 else 0 end + 
case when nevenvestigingen is not null then 1 else 0 end + 
case when telefoonnummer is not null then 1 else 0 end + 
case when fax is not null then 1 else 0 end + 
case when email is not null then 1 else 0 end + 
case when website is not null then 1 else 0 end + 
case when twitter is not null then 1 else 0 end + 
case when facebook is not null then 1 else 0 end + 
case when youtube is not null then 1 else 0 end + 
case when linkedin is not null then 1 else 0 end + 
case when hoofdrubriek is not null then 1 else 0 end + 
case when subrubrieken is not null then 1 else 0 end + 
case when merken is not null then 1 else 0 end +
case when diensten is not null then 1 else 0 end +
case when productsoorten is not null then 1 else 0 end +
case when brancheverenigingen is not null then 1 else 0 end

 DESC"; 

1 个答案:

答案 0 :(得分:0)

也许bedrijfslogo在表中是空的? :)

如果您将外壳添加到视图中,则可能更容易找到错误。

create view myView as 
select *, (CASE when ID is not null then 1 else 0 end + ... n) AS SortPoints
from table 
GO
select * from myView order by SortPoints desc