doumeba0486 2014-03-26 08:26
浏览 325
已采纳

MYSQL - 选择值等于any的元素

Imagine a simple query, where $value is a boolean variable:

"SELECT * FROM table where table.value = {$value}";

It's easy to filter the selection for $value equal to true or equal to false.

The question is: how could I filter by ANY (true or false) in the same query?

  • 写回答

2条回答 默认 最新

  • doucheng9634 2014-03-26 09:33
    关注

    Sets of values and comparison

    There's a field of some data type which can take some set of values (ENUM is very good example, but not only example). So if you want to compare by some subset of values, you can use IN:

    SELECT * FROM t WHERE v IN ($values)
    

    here $values stands for set of values, not one value. Set of values is delimited by comma, thus, if you have array:

    $values = [1, 2, 5];
    

    then you need to query like:

    //values escaping is needed, but out of this issue:
    $query = 'SELECT * FROM t WHERE v IN ('.join(',', $values).')';
    

    Also, string literals have to be enclosed by quotes - therefore, construction above should be modified for them like:

    $values = ['foo', 'bar'];
    $inSet  = array_map(function($x)
    {
       return '"'.$x.'"';
    }, $values);
    $query = 'SELECT * FROM t WHERE v IN ('.join(',', $inSet).')';
    

    Current case

    There is, however, one case when comparison above won't be needed. It's like your question. In your case, your data type full set is equal to subset of values in comparison. Thus, whole condition makes no sense - because data type can not hold anything out of it's full set of values. Thus, you can either omit your condition at all or leave WHERE 1 (if having WHERE is mandatory).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?