谁能告诉我如何编写SQL

时间:2013-12-04 03:40:55

标签: mysql sql

这就是我写的:

$sql = "select * from tp_picture where (catid in(13,14,15,20) order by find_in_set(catid,'13,14,15,20')) order by listorder ASC";

首先,按find_in_set排序(catid,'13,14,15,20'))

接下来,按tp_picture listorder字段排序。

谁能告诉我正确的SQL,思考。

4 个答案:

答案 0 :(得分:2)

如果我理解你想要的东西,那就试着这样做

select * 
  from tp_picture 
 where catid in(13, 14, 15, 20) 
 order by (find_in_set(catid, 13, 14, 15, 20) > 0) DESC, listorder

答案 1 :(得分:0)

试试这个。

SELECT * FROM TP_PICTURE 
WHERE CAT_ID IN (13,14,15,20) AS 'CAT_ID'
ORDER BY CAT_ID, LISTORDER ASC

答案 2 :(得分:0)

Please, follow below syntex :
  select <column_name1, column_name2, ... >
       from table_name where <condition>
           order by <column_name1,...>
------------------------------------------------------------
    $sql = "select * from tp_picture 
    where catid in(13,14,15,20) 
    order by  catid , listorder ASC";

答案 3 :(得分:-2)

$sql = "select 
               *
        from 
                tp_picture 
        where 
                (catid in(13,14,15,20)) 
        order by 
                catid,listorder ASC";

试试这个