【问题标题】:Query based on an input array and get the same result in the same order根据输入数组进行查询,以相同的顺序得到相同的结果
【发布时间】:2014-09-08 14:36:23
【问题描述】:

我想获取特定 uuid 的场景 ID 数组的活动状态;例如输入将是scene_id in(1,2,3)和uuid=2,所以它应该以传递scene_id的相同顺序返回(1,0,1)

scene_id  uuid active
1           2   1
3           2   1
5           2   1
7           2   1
1           3   1
2           3   1
5           3   1
5           4   1
1           4   1
1           8   0

我试过了:

select active from likes where scene_id in (1,2,3) and uuid=2

但它返回 (1,1);它忽略 0,因为没有 scene_id=2 和 uuid=2 如果您需要更多说明,请告诉我!

【问题讨论】:

  • 示例应该返回 (1, 0, 1),而不是 (1, 1, 0),对吧?
  • 哦,对了!我会纠正它谢谢!

标签: mysql sql mysqli mysql-workbench


【解决方案1】:

如果您可以将特定的 uuid 记录插入到新表中,那就更容易了:

insert into newTable
    select scene_id, uid
        from likes
        where scene_id in(1,2,3) and uuid=2

那么你的查询应该是这样的:

select ifNull(active, 0) as r 
from
    likes
        left join newTable on likes.scene_id=newTable.sceneId and likes.uuid=newTable.uuid

您还可以指定最后一个查询的特定顺序。

【讨论】:

    猜你喜欢
    • 2013-08-22
    • 2014-11-20
    • 2014-07-06
    • 2023-01-19
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    相关资源
    最近更新 更多