繁体   English   中英

SQL - 从多个值字段中选择

[英]SQL - Selecting from a multiples values field

我正在使用 Teampass,这是表架构的最小化示例

Teampass_Roles_Title :

+----+----------------------+
| id | title                |
+----+----------------------+
|  1 | Title 1              |
|  2 | Title 2              |
|  3 | Title 3              |
|  4 | Title 4              |
|  5 | Title 5              |
|  6 | Title 6              |
  ...
+----+----------------------+

和 Teampass_Users

+------------+------------------------------------------------------------+
| login      | fonction_id                                                |
+------------+------------------------------------------------------------+
| AAA        |                                                            |
| BBB        |                                                            |
| CCC        | 12;21;                                                     |
| DDD        | 3;4;5;6;7;8;9;14;15;                                       |
| EEE        | 3;7;                                                       |
| FFF        | 3;7;                                                       |
| GGG        | 3;4;5;6;7;8;9;14;15;21;                                    |
| HHH        | 3;4;5;6;7;8;9;14;15;21;                                    |
  ...
+------------+------------------------------------------------------------+

fonction_id中的数字是Teampass_Roles_Title表中的 id 他们没有使用中间表,而是选择了这种模型,我不知道是否有任何方法可以获得例如访问Title 6的所有用户的列表

+------------+------------------------------------------------------------+
| login      | fonction_id                                                |
+------------+------------------------------------------------------------+
| DDD        | 3;4;5;6;7;8;9;14;15;                                       |
| GGG        | 3;4;5;6;7;8;9;14;15;21;                                    |
| HHH        | 3;4;5;6;7;8;9;14;15;21;                                    |
  ...
+------------+------------------------------------------------------------+

这是一个非常糟糕的数据模型。 有时,我们会被其他人非常非常糟糕的决定所困扰。 您可以使用find_in_set()replace()执行您想要的操作:

where find_in_set(6, replace(function_id, ';', ',')) > 0

至少可以说,数据库模型不是很好。 可以做你需要的查询是:

select
  u.*
from Teampass_Roles_Title t
join Teampass_Users u on u.fonction_id like concat('', t.id, ';%')
                      or u.fonction_id like concat('%;', t.id, ';%')
where t.title = 'Title 6'

但是,这在资源使用方面不是最佳的。 除非您修复数据库模型,否则我看不到解决方法。

使用运算符LIKE

select * from Teampass_Users
where 
  concat(';', fonction_id, ';') 
  like 
  concat('%;', (select id from Teampass_Roles_Title where title = 'Title 6'), ';%')

请参阅演示
结果:

| login | fonction_id             |
| ----- | ----------------------- |
| DDD   | 3;4;5;6;7;8;9;14;15;    |
| GGG   | 3;4;5;6;7;8;9;14;15;21; |
| HHH   | 3;4;5;6;7;8;9;14;15;21; |

你也可以像这样修改LIKE

where function_id like '%;%6%;%' or function_id like '6%;%'

暂无
暂无

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

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