jookr
V2EX  ›  问与答

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

  •  
  •   jookr · Mar 28, 2015 · 4278 views
    This topic created in 4259 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
    liuhaotian
    Mar 28, 2015
    1
    21,1,3,22有什么规律吗?如果有的话可以以这个为规则
    jookr
    jookr
    OP
    Mar 28, 2015
    2
    自己查到方法了
    order by field(userid,21,1,3,22)
    huijiewei
    huijiewei
    Mar 28, 2015
    3
    MySQL 的话可以用 FIND_IN_SET
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4688 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:24 · PVG 14:24 · LAX 22:24 · JFK 01:24
    ♥ Do have faith in what you're doing.