繁体   English   中英

在MySQL中执行find_in_set()

[英]Execution of find_in_set() in mysql

这是我的屏幕截图,显示了使用命令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)

我选择了530颜色以及monday , friday天为单位),但只显示了13行。

谁能纠正我的问题?

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

这些运算符具有默认的优先顺序

当您编写此代码时:

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()没有关系。 这只是布尔代数错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM