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

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

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

      1. MYSQL 连接逗号分隔查询

        时间:2023-10-09
            <bdo id='ZWRLb'></bdo><ul id='ZWRLb'></ul>

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

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

                • 本文介绍了MYSQL 连接逗号分隔查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我四处寻找,一无所获.

                  I have searched around and came up with nothing.

                  我有 2 个表,不必为每篇显示我需要以某种方式加入它们的帖子查询数据库.

                  I have 2 tables and to not have to query the database for every post that shows i need to join them somehow.

                  我想从具有 post 表中 pics 字段的 id 的 pics 表中获取 url.现在我的问题是:pics 字段是一个逗号分隔的列表"(4,1 或 32,4,32,2),因为每个帖子通常有不止一张图片.

                  I want to get the url from the pics table that have the id of the pics field in posts table. Now heres my problem: the pics field is a commma separated "list" (4,1 or 32,4,32,2), because every post usually have more than one picture.

                  表设置:

                  帖子:

                   id | header | text | pics
                  | 1     xxx     xxx    3,1     
                  | 2     xxx     xxx    2,10,4     
                  | 3     xxx     xxx    16,17,18,19     
                  | 4     xxx     xxx    11,12,13        
                  

                  图片:

                  id | name | url
                  | 1   xxx   xxx    
                  | 2   xxx   xxx        
                  | 3   xxx   xxx          
                  | 4   xxx   xxx          
                  | 10  xxx   xxx         
                  | 11  xxx   xxx         
                  | 12  xxx   xxx                  
                  | 13  xxx   xxx          
                  | 16  xxx   xxx          
                  | 17  xxx   xxx        
                  | 18  xxx   xxx        
                  

                  推荐答案

                  强烈建议您修复当前的数据库结构,以免将数据存储在逗号分隔的列表中.您应该按照如下方式构建表格:

                  I strongly advise that you fix your current database structure so you are not storing the data in a comma separated list. You should structure your tables similar to the following:

                  CREATE TABLE posts
                      (`id` int, `header` varchar(3), `text` varchar(3))
                  ;
                  
                  CREATE TABLE pics
                      (`id` int, `name` varchar(3), `url` varchar(3))
                  ;
                  
                  CREATE TABLE post_pics
                      (`post_id` int, `pic_id` int)
                  ;
                  

                  然后您可以通过加入表格轻松获得结果:

                  Then you can easily get a result by joining the tables:

                  select p.id,
                    p.header,
                    p.text,
                    c.name,
                    c.url
                  from posts p
                  inner join post_pics pp
                    on p.id = pp.post_id
                  inner join pics c
                    on pp.pic_id = c.id;
                  

                  参见 SQL Fiddle 和演示.

                  如果你不能改变你的表,那么你应该能够使用FIND_IN_SET进行查询:

                  If you cannot alter your table, then you should be able to query using FIND_IN_SET:

                  select p.id, p.header, p.text, p.pics,
                    c.id c_id, c.name, c.url
                  from posts p
                  inner join pics c
                    on find_in_set(c.id, p.pics)
                  

                  参见SQL Fiddle with Demo.

                  编辑,如果您希望数据显示为逗号分隔的列表,那么您可以使用GROUP_CONCAT.

                  Edit, if you want the data displayed as a comma-separated list then you can use GROUP_CONCAT.

                  查询 1:

                  select p.id,
                    p.header,
                    p.text,
                    group_concat(c.name separator ', ') name,
                    group_concat(c.url separator ', ') url
                  from posts p
                  inner join post_pics pp
                    on p.id = pp.post_id
                  inner join pics c
                    on pp.pic_id = c.id
                  group by p.id, p.header, p.text;
                  

                  参见SQL Fiddle with Demo

                  查询 2:

                  select p.id, p.header, p.text, p.pics,
                    group_concat(c.name separator ', ') name,
                    group_concat(c.url separator ', ') url
                  from posts p
                  inner join pics c
                    on find_in_set(c.id, p.pics)
                  group by p.id, p.header, p.text, p.pics;
                  

                  参见SQL Fiddle with Demo

                  这篇关于MYSQL 连接逗号分隔查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:“使用"和“使用"有什么区别?和“开"在 MySQL 中的表连接中? 下一篇:如何在 MySQL 中完全 OUTER JOIN 多个表

                  相关文章

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

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

                    2. <legend id='UZM15'><style id='UZM15'><dir id='UZM15'><q id='UZM15'></q></dir></style></legend>