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

      <legend id='Elcib'><style id='Elcib'><dir id='Elcib'><q id='Elcib'></q></dir></style></legend>

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

        • <bdo id='Elcib'></bdo><ul id='Elcib'></ul>
        <tfoot id='Elcib'></tfoot>

        如何在连接字段中使用逗号分隔列表连接两个表

        时间:2023-05-23
        <i id='Jps2W'><tr id='Jps2W'><dt id='Jps2W'><q id='Jps2W'><span id='Jps2W'><b id='Jps2W'><form id='Jps2W'><ins id='Jps2W'></ins><ul id='Jps2W'></ul><sub id='Jps2W'></sub></form><legend id='Jps2W'></legend><bdo id='Jps2W'><pre id='Jps2W'><center id='Jps2W'></center></pre></bdo></b><th id='Jps2W'></th></span></q></dt></tr></i><div id='Jps2W'><tfoot id='Jps2W'></tfoot><dl id='Jps2W'><fieldset id='Jps2W'></fieldset></dl></div>

      1. <legend id='Jps2W'><style id='Jps2W'><dir id='Jps2W'><q id='Jps2W'></q></dir></style></legend>

            <bdo id='Jps2W'></bdo><ul id='Jps2W'></ul>
              <tbody id='Jps2W'></tbody>

                  <tfoot id='Jps2W'></tfoot>

                • <small id='Jps2W'></small><noframes id='Jps2W'>

                • 本文介绍了如何在连接字段中使用逗号分隔列表连接两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有两个表,categoriesmovies.

                  movies 表中,我有一列categories.该列由电影所属的类别组成.类别是用逗号分隔的 ID.

                  In movies table I have a column categories. That column consists of the categories that movie fits in. The categories are IDs separated by a comma.

                  这是一个例子:

                  Table categories {
                    -id-       -name-
                    1          Action
                    2          Comedy
                    4          Drama
                    5          Dance
                  }
                  
                  Table movies {
                    -id-       -categories-  (and some more columns ofc)
                    1          2,4
                    2          1,4
                    4          3,5
                  }
                  

                  现在到实际问题:是否可以执行从电影表中排除类别列的查询,而是从类别表中选择匹配的类别并在数组中返回它们?像join一样,但问题是有多个用逗号分隔的类别,是否可以做某种正则表达式?

                  Now to the actual question: Is it possible to perform a query that excludes the categories column from the movies table, and instead selects the matching categories from the categories table and returns them in an array? Like a join, but the problem is there are multiple categories separated by comma, is it possible to do some kind of regex?

                  推荐答案

                  在数据库字段中使用逗号分隔列表是一种反模式,应不惜一切代价避免.
                  因为在 SQL 中再次提取这些逗号分隔的值是一个 PITA.

                  Using comma separated lists in a database field is an anti-pattern and should be avoided at all costs.
                  Because it is a PITA to extract those comma separated values out agian in SQL.

                  相反,您应该添加一个单独的链接表来表示类别和电影之间的关系,如下所示:

                  Instead you should add a separate link table to represent the relationship between categories and movies, like so:

                  Table categories
                    id integer auto_increment primary key
                    name varchar(255)
                  
                  Table movies
                    id integer auto_increment primary key
                    name varchar(255)
                  
                  Table movie_cat
                    movie_id integer foreign key references movies.id
                    cat_id integer foreign key references categories.id
                    primary key (movie_id, cat_id)
                  

                  现在可以了

                  SELECT m.name as movie_title, GROUP_CONCAT(c.name) AS categories FROM movies m
                  INNER JOIN movie_cat mc ON (mc.movie_id = m.id)
                  INNER JOIN categories c ON (c.id = mc.cat_id)
                  GROUP BY m.id
                  

                  回到你的问题
                  或者,您可以使用您的数据

                  Back to your question
                  Alternativly using your data you can do

                  SELECT m.name as movie_title
                    , CONCAT(c1.name, if(c2.name IS NULL,'',', '), ifnull(c2.name,'')) as categories 
                  FROM movies m
                  LEFT JOIN categories c2 ON 
                   (replace(substring(substring_index(m.categories, ',', 2),
                    length(substring_index(m.categories, ',', 2 - 1)) + 1), ',', '') = c2.id)
                  INNER JOIN categories c1 ON 
                   (replace(substring(substring_index(m.categories, ',', 1), 
                    length(substring_index(m.categories, ',', 1 - 1)) + 1), ',', '') = c1.id)
                  

                  请注意,最后一个查询仅适用于每部电影有 2 个或更少类别的情况.

                  Note that the last query only works if there are 2 or fewer categories per movie.

                  这篇关于如何在连接字段中使用逗号分隔列表连接两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将列值设置为 SQL 查询结果中的列名 下一篇:LOAD DATA LOCAL INFILE 给出错误 The used command is not allowed

                  相关文章

                  <legend id='vGQki'><style id='vGQki'><dir id='vGQki'><q id='vGQki'></q></dir></style></legend>

                  <tfoot id='vGQki'></tfoot>

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

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