SQL通过通配符排序

时间:2013-02-16 02:21:12

标签: mysql sql wildcard

我有这个MySQL查询:

$query = mysql_query("SELECT * FROM school_staff WHERE DELETED = 'NO' AND BUILDING = '$building' ORDER by 
    Department != 'Principal',
    Department != 'Assistant Principal', 
    Department != 'Kindergarten', 
    Department != 'First Grade',
    Department != 'Second Grade',
    Department != 'Third Grade',
    Department != 'Fourth Grade',
    Department != 'Fifth Grade',
    Department != 'Learning Support',
    Department != 'Alternative Education', 
    Department != '% - Chair', 
    Department ASC") or die(mysql_error());

如何制作该行:Department != '% - Chair',以使%任何文字?

3 个答案:

答案 0 :(得分:1)

编辑以回应您的评论:

我会使用CASE语句。像

这样的东西
ORDER BY CASE WHEN Department LIKE '% - Chair' THEN 1 WHEN Department = 'Alternative Education' THEN 2 ELSE 3 END

您可以根据需要在CASE语句中添加尽可能多的WHEN语句。

答案 1 :(得分:0)

您是否尝试过部门不喜欢'%主席%

答案 2 :(得分:0)

在这种情况下使用SQL like运算符。

试试这个:

Department NOT LIKE '% - Chair'