• <bdo id='l8gyE'></bdo><ul id='l8gyE'></ul>

      <i id='l8gyE'><tr id='l8gyE'><dt id='l8gyE'><q id='l8gyE'><span id='l8gyE'><b id='l8gyE'><form id='l8gyE'><ins id='l8gyE'></ins><ul id='l8gyE'></ul><sub id='l8gyE'></sub></form><legend id='l8gyE'></legend><bdo id='l8gyE'><pre id='l8gyE'><center id='l8gyE'></center></pre></bdo></b><th id='l8gyE'></th></span></q></dt></tr></i><div id='l8gyE'><tfoot id='l8gyE'></tfoot><dl id='l8gyE'><fieldset id='l8gyE'></fieldset></dl></div>
      <tfoot id='l8gyE'></tfoot>
    1. <legend id='l8gyE'><style id='l8gyE'><dir id='l8gyE'><q id='l8gyE'></q></dir></style></legend>
      1. <small id='l8gyE'></small><noframes id='l8gyE'>

        MySQL :: 从逗号分隔的字符串中选择

        时间:2023-10-26

              • <tfoot id='dANGi'></tfoot>
                  <tbody id='dANGi'></tbody>
                  <bdo id='dANGi'></bdo><ul id='dANGi'></ul>
                  <i id='dANGi'><tr id='dANGi'><dt id='dANGi'><q id='dANGi'><span id='dANGi'><b id='dANGi'><form id='dANGi'><ins id='dANGi'></ins><ul id='dANGi'></ul><sub id='dANGi'></sub></form><legend id='dANGi'></legend><bdo id='dANGi'><pre id='dANGi'><center id='dANGi'></center></pre></bdo></b><th id='dANGi'></th></span></q></dt></tr></i><div id='dANGi'><tfoot id='dANGi'></tfoot><dl id='dANGi'><fieldset id='dANGi'></fieldset></dl></div>

                  <small id='dANGi'></small><noframes id='dANGi'>

                  <legend id='dANGi'><style id='dANGi'><dir id='dANGi'><q id='dANGi'></q></dir></style></legend>
                • 本文介绍了MySQL :: 从逗号分隔的字符串中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下表格:

                  过滤器

                  id | u_ids
                  1  | 1, 2, 3
                  2  | 5, 6
                  

                  用户

                  id | name
                  1  | Tom
                  2  | Tim
                  3  | Sue
                  4  | Bruce
                  5  | Ann
                  6  | George
                  

                  我想运行以下选择

                  select * from users where id in (select u_ids from filters where id =1);
                  

                  我想收到

                  id | name
                  1  | Tom
                  2  | Tim
                  3  | Sue
                  

                  但我什么也没收到.

                  问题在于字段 u_ids 是一个文本,因此in select"返回的内容类似于1, 2, 3"(带分号),因此 in 未找到任何值.

                  The problem is that the field u_ids is a text so the "in select" is returning something like "1, 2, 3" (with the semicolon) so the in does not find any value.

                  是否有任何选项可以进行转换或将字符串更改为数组?

                  Is there any option to make a casting or something to change the string to an array?

                  推荐答案

                  最好是规范化你的模式,不要以逗号分隔列表的形式存储关系,而是为此创建一个连接表来维护一个 m:m 用户和过滤器之间的多对多关系,创建一个新表作为 user_filters 列过滤器 id 和用户 id,并在每一行中为每个用户保存一个关联,并像您当前的架构关系一样进行过滤对于有很多用户的过滤器 1 (1, '1, 2, 3') 将变成

                  Its better to normalize your schema do not store relations in form of comma separated list instead create a junction table for this like to maintain a m:m many to many relation between users and filters,create a new table as user_filters with columns filter id and user id and in each row save one association per user and filter like in your current schema relation for filter 1 with many users (1, '1, 2, 3') will become like

                  filter id user id
                      (1, '1'),
                      (1, '2'),
                      (1, '3'),
                  

                  <小时>

                  示例架构将是这样的


                  Sample schema will be like this

                  CREATE TABLE user_filters
                      (`fid` int, `u_id` varchar(50))
                  ;
                  
                  INSERT INTO user_filters
                      (`fid`, `u_id`)
                  VALUES
                      (1, '1'),
                      (1, '2'),
                      (1, '3'),
                      (2, '5'),
                      (2, '5')
                  ;
                  
                  CREATE TABLE filters
                      (`id` int, `title` varchar(50))
                  ;
                  
                  INSERT INTO filters
                      (`id`, `title`)
                  VALUES
                      (1, 'test'),
                      (2, 'test 1')
                  ;
                  
                  
                  CREATE TABLE users
                      (`id` int, `name` varchar(6))
                  ;
                  
                  INSERT INTO users
                      (`id`, `name`)
                  VALUES
                      (1, 'Tom'),
                      (2, 'Tim'),
                      (3, 'Sue'),
                      (4, 'Bruce'),
                      (5, 'Ann'),
                      (6, 'George')
                  ;
                  

                  对于上述模式,您可以使用 join as 轻松查询,以下查询可以使用索引进行优化

                  For above schema you can easily query with join as, below query can be optimized using indexes

                  select u.* 
                  from users u
                  join user_filters uf on(uf.u_id = u.id)
                   where uf.fid =1
                  

                  示例演示

                  <小时>

                  如果您无法更改您的架构并想坚持使用当前的架构,您可以按如下方式查询,但与上述查询相比,此架构无法优化

                  Sample Demo


                  If you are not able to alter your schema and want to stick with the current one you can query as below but this one cannot be optimized enough as compare to above query

                  select u.* 
                  from users u
                  join filters f on(find_in_set(u.id,replace(`u_ids`,' ','')) > 0)
                   where f.id =1 
                  

                  这篇关于MySQL :: 从逗号分隔的字符串中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Mysql 按组计数出现次数 下一篇:列表中的 mysql 仅验证列表中的第一个 id.也许是斑点问题

                  相关文章

                  <legend id='jUMqJ'><style id='jUMqJ'><dir id='jUMqJ'><q id='jUMqJ'></q></dir></style></legend>
                    <bdo id='jUMqJ'></bdo><ul id='jUMqJ'></ul>

                    <small id='jUMqJ'></small><noframes id='jUMqJ'>

                      <i id='jUMqJ'><tr id='jUMqJ'><dt id='jUMqJ'><q id='jUMqJ'><span id='jUMqJ'><b id='jUMqJ'><form id='jUMqJ'><ins id='jUMqJ'></ins><ul id='jUMqJ'></ul><sub id='jUMqJ'></sub></form><legend id='jUMqJ'></legend><bdo id='jUMqJ'><pre id='jUMqJ'><center id='jUMqJ'></center></pre></bdo></b><th id='jUMqJ'></th></span></q></dt></tr></i><div id='jUMqJ'><tfoot id='jUMqJ'></tfoot><dl id='jUMqJ'><fieldset id='jUMqJ'></fieldset></dl></div>
                      <tfoot id='jUMqJ'></tfoot>