jookr
V2EX  ›  问与答

SQL 查询 where in 查出来的顺序怎么按照 in 里的顺序呢?

  •  
  •   jookr · Mar 28, 2015 · 3964 views
    This topic created in 4088 days ago, the information mentioned may be changed or developed.

    select * from yw_company where userid in (21,1,3,22)
    userid是主键
    查出来的数据是按照userid升序的
    怎么写语句才能按照in里的顺序
    也就是
    第一条userid是21
    第二条userid是1
    第三条userid是3
    第四条userid是22
    这顺序排列呢

    3 replies    2015-03-28 17:40:51 +08:00
    liuhaotian
        1
    liuhaotian  
       Mar 28, 2015
    21,1,3,22有什么规律吗?如果有的话可以以这个为规则
    jookr
        2
    jookr  
    OP
       Mar 28, 2015
    自己查到方法了
    order by field(userid,21,1,3,22)
    huijiewei
        3
    huijiewei  
       Mar 28, 2015
    MySQL 的话可以用 FIND_IN_SET
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2557 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 01:40 · PVG 09:40 · LAX 18:40 · JFK 21:40
    ♥ Do have faith in what you're doing.