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

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

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

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

        JOIN 和 GROUP_CONCAT 与三个表

        JOIN and GROUP_CONCAT with three tables(JOIN 和 GROUP_CONCAT 与三个表)

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

          <tbody id='cXt18'></tbody>

              <bdo id='cXt18'></bdo><ul id='cXt18'></ul>
            • <tfoot id='cXt18'></tfoot>
                <i id='cXt18'><tr id='cXt18'><dt id='cXt18'><q id='cXt18'><span id='cXt18'><b id='cXt18'><form id='cXt18'><ins id='cXt18'></ins><ul id='cXt18'></ul><sub id='cXt18'></sub></form><legend id='cXt18'></legend><bdo id='cXt18'><pre id='cXt18'><center id='cXt18'></center></pre></bdo></b><th id='cXt18'></th></span></q></dt></tr></i><div id='cXt18'><tfoot id='cXt18'></tfoot><dl id='cXt18'><fieldset id='cXt18'></fieldset></dl></div>
                  <legend id='cXt18'><style id='cXt18'><dir id='cXt18'><q id='cXt18'></q></dir></style></legend>
                  本文介绍了JOIN 和 GROUP_CONCAT 与三个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有三张桌子:

                  users:        sports:           user_sports:
                  
                  id | name     id | name         id_user | id_sport | pref
                  ---+--------  ---+------------  --------+----------+------
                   1 | Peter     1 | Tennis             1 |        1 |    0
                   2 | Alice     2 | Football           1 |        2 |    1
                   3 | Bob       3 | Basketball         2 |        3 |    0
                                                        3 |        1 |    2
                                                        3 |        3 |    1
                                                        3 |        2 |    0
                  

                  表格 user_sportsuserssports 与偏好顺序 (pref) 联系起来.

                  The table user_sports links users and sports with an order of preference (pref).

                  我需要做一个返回这个的查询:

                  I need to make a query that returns this:

                  id | name  | sport_ids | sport_names
                  ---+-------+-----------+----------------------------
                   1 | Peter | 1,2       | Tennis,Football
                   2 | Alice | 3         | Basketball
                   3 | Bob   | 2,3,1     | Football,Basketball,Tennis
                  

                  我尝试过 JOINGROUP_CONCAT,但结果很奇怪.
                  我需要进行嵌套查询吗?
                  有什么想法吗?

                  I have tried with JOIN and GROUP_CONCAT but I get weird results.
                  Do I need to do a nested query?
                  Any ideas?

                  推荐答案

                  并不是特别困难.

                  1. 使用 JOIN 子句连接三个表.
                  2. 在您感兴趣的领域使用 Group_concat.
                  3. 不要忘记未连接的字段上的 GROUP BY 子句,否则会发生奇怪的事情


                  SELECT u.id, 
                         u.Name, 
                         Group_concat(us.id_sport order by pref) sport_ids, 
                         Group_concat(s.name order by pref)      sport_names 
                  FROM   users u 
                         LEFT JOIN User_Sports us 
                                 ON u.id = us.id_user 
                         LEFT  JOIN sports s 
                                 ON US.id_sport = s.id 
                  GROUP  BY u.id, 
                            u.Name 
                  

                  演示

                  更新 LEFT JOIN 用于当用户根据评论在 User_Sports 中没有条目时

                  Update LEFT JOIN for when the user doesn't have entries in User_Sports as per comments

                  这篇关于JOIN 和 GROUP_CONCAT 与三个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Building a comma separated list?(建立一个逗号分隔的列表?)
                  Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                  How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                  Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                  Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                  SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)

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

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

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