【问题标题】:Execution of find_in_set() in mysql在mysql中执行find_in_set()
【发布时间】:2017-02-10 06:16:56
【问题描述】:

这是我的屏幕截图,显示了使用命令 find_in_set 执行的查询。

它只执行特定行中的一组值。我想在单个查询中执行与find_in_set 命令匹配的表中的所有值。

看看我的代码:

select * from shirts;
+----+--------------+------------------------------------+
| id | colors       | days                               |
+----+--------------+------------------------------------+
|  1 | 1,2,5,12,15  | monday, friday, thursday           |
|  2 | 1,5,12,15,30 | tuesday,monday                     |
|  3 | 2,5,11,15,28 | monday, friday,wednesday ,thursday |
|  4 | 1,2,7,12,15  | tuesday,monday                     |
|  5 | 2,4,8,12,15  | tuesday,monday                     |
+----+--------------+------------------------------------+
5 rows in set (0.00 sec)

mysql> select * from shirts where find_in_set('5',colors) or find_in_set('30',colors) or find_in_set('30', colors) and find_in_set('monday',days) or  find_in_set('tuesday',days);
+----+--------------+------------------------------------+
| id | colors       | days                               |
+----+--------------+------------------------------------+
|  1 | 1,2,5,12,15  | monday, friday, thursday           |
|  2 | 1,5,12,15,30 | tuesday,monday                     |
|  3 | 2,5,11,15,28 | monday, friday,wednesday ,thursday |
|  4 | 1,2,7,12,15  | tuesday,monday                     |
|  5 | 2,4,8,12,15  | tuesday,monday                     |
+----+--------------+------------------------------------+
5 rows in set (0.00 sec)

mysql> select * from shirts where find_in_set('5',colors) or find_in_set('30',colors) and find_in_set('monday',days) or  find_in_set('tuesday',days);
+----+--------------+------------------------------------+
| id | colors       | days                               |
+----+--------------+------------------------------------+
|  1 | 1,2,5,12,15  | monday, friday, thursday           |
|  2 | 1,5,12,15,30 | tuesday,monday                     |
|  3 | 2,5,11,15,28 | monday, friday,wednesday ,thursday |
|  4 | 1,2,7,12,15  | tuesday,monday                     |
|  5 | 2,4,8,12,15  | tuesday,monday                     |
+----+--------------+------------------------------------+
5 rows in set (0.00 sec)

我在colors中选择了530,在days中选择了monday , friday,但它只显示13行。 p>

谁能纠正我的问题?

【问题讨论】:

  • 你的截图在哪里?
  • 改进格式,修正语法,添加标签

标签: mysql sql operator-precedence


【解决方案1】:

请记住,在布尔代数中,AND 的绑定比 OR 更紧密。

有一个default order of precedence of these operators

当你写这个时:

select * from shirts where A or B and C or D

你总是像这样使用括号:

select * from shirts where A or (B and C) or D

但你希望它表现得像这样:

select * from shirts where (A or B) and (C or D)

因此,您必须使用显式括号编写查询,以覆盖 ANDOR 的默认优先顺序。

这个问题与使用 find_in_set() 无关。这只是一个布尔代数错误。

【讨论】:

    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 2021-07-31
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多