【问题标题】:How to write a MySql select query where id NOT IN list, that can be empty如何编写一个 MySql 选择查询,其中 id NOT IN 列表,可以为空
【发布时间】:2019-07-14 19:45:30
【问题描述】:

我有一个用例从表中选择 id,其中名称不应出现在动态生成的列表中,有时可能为空

@SqlQuery("SELECT id FROM APP_TABLE a WHERE a.app_id = :appId AND a.name NOT IN (<nameList>)")
public List<Long> getIds(@Bind("appId") String appId, @BindIn(value = "nameList") List<String> nameList);

只要 nameList 为空,就会引发异常。请建议修改查询以实现该用例。

【问题讨论】:

  • 如果&lt;nameList&gt; 来自另一个查询,SELECT id FROM APP_TABLE a LEFT JOIN othertable ON a.name = othertable.name {..other critiera} WHERE othertable.name IS NULL 会更有效

标签: java mysql sql database where-in


【解决方案1】:

如果列表为空,只需在列表中添加空白, if(nameList.isEmpty()) nameList.add("");

【讨论】:

    猜你喜欢
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多