• <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 连接逗号分隔查询

        MYSQL join comma separated query(MYSQL 连接逗号分隔查询)
            <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 连接逗号分隔查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to update a record using sequelize for node?(如何使用节点的 sequelize 更新记录?)
                  How to provide a mysql database connection in single file in nodejs(如何在 nodejs 中的单个文件中提供 mysql 数据库连接)
                  Looping Over Result Sets in MySQL(在 MySQL 中循环结果集)
                  What are the advantages of VistaDB(VistaDB有什么优势)
                  SQLite insert speed slows as number of records increases due to an index(SQLite 插入速度随着索引数量的增加而变慢)
                  sqlite select with condition on date(sqlite 选择日期条件)

                    <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>

                        • <legend id='UZM15'><style id='UZM15'><dir id='UZM15'><q id='UZM15'></q></dir></style></legend>
                            <tbody id='UZM15'></tbody>