MySQL group_concat with select inside select

标签 mysql

我有一个我无法解决的故障,让我详细说明...

这些是我的 MySQL 表...

治疗师表

id  therapist_name
1   Therapist 1
2   Therapist 2 

位置表

+-----+------------+--+
| id  |    name    |  |
+-----+------------+--+
|  1  | Location 1 |  |
|  2  | Location 2 |  |
|  3  | Location 3 |  |
+-----+------------+--+

Days_location 表

+-----+-----------+--------------+-------------+--+
| id  |    day    | therapist_id | location_id |  |
+-----+-----------+--------------+-------------+--+
|  1  | monday    |            1 |           1 |  |
|  2  | monday    |            1 |           2 |  |
|   3 | wednesday |            1 |           3 |  |
|   4 | wednesday |            2 |           1 |  |
|   5 | tuesday   |            2 |           2 |  |
|   6 | friday    |            2 |           1 |  |
|   7 | friday    |            2 |           2 |  |
|   8 | friday    |            1 |           1 |  |
+-----+-----------+--------------+-------------+--+

现在我想让每个治疗师每天都有位置,例如这样的:

therapist_name=>Therapist 1,day_locations=>monday(Location1,Location2),friday(Location1)

我需要它作为一个选择变量,这是我的查询,但我被困在那里:

SELECT t.*,GROUP_CONCAT(
    SELECT CONCAT(dl2.day,GROUP_CONCAT(dl2.location_id)) as concated 
    FROM days_location dl2 
    WHERE therapist_id=85 
    GROUP BY dl2.day
) as day_location 
FROM therapists t  
LEFT JOIN days_location dl 
ON dl.therapist_id=t.id

这当然行不通,我做错了什么......我应该尝试不同的方法还是让我的表不同?

最佳答案

我相信这就是您正在寻找的,或者可以让您开始:

SELECT
    t.therapist_name,
    dl.day,
    GROUP_CONCAT(DISTINCT dl.name SEPARATOR ',') AS locations
FROM
    therapists t 
    LEFT JOIN days_location dl ON dl.therapist_id = t.id
    LEFT JOIN location l ON dl.location_id = l.id
GROUP BY t.therapist_name, dl.day

对于 therapists.id = 1 这应该给你结果:

+----------------+-----------+-----------------------+
| therapist_name |    day    |       locations       |
+----------------+-----------+-----------------------+
| Therapist 1    | monday    | Location 1,Location 2 |
| Therapist 1    | wednesday | Location 3            |
| Therapist 1    | friday    | Location 1            |
+----------------+-----------+-----------------------+

如果您需要将 daylocations 列连接起来,请使用简单的 CONCAT():

SELECT
    therapist_name,
    CONCAT(day, '(', locations, ')') AS locations
FROM (  
    SELECT
        t.therapist_name,
        dl.day,
        GROUP_CONCAT(DISTINCT dl.name SEPARATOR ',') AS locations
    FROM
        therapists t 
        LEFT JOIN days_location dl ON dl.therapist_id = t.id
        LEFT JOIN location l ON dl.location_id = l.id
    GROUP BY t.therapist_name, dl.day
    ) t
GROUP BY therapist_name, locations

输出应该是这样的:

+----------------+-------------------------------+
| therapist_name |           locations           |
+----------------+-------------------------------+
| Therapist 1    | monday(Location 1,Location 2) |
| Therapist 1    | wednesday(Location 3)         |
| Therapist 1    | friday(Location 1)            |
+----------------+-------------------------------+

如果您需要为每位治疗师将其全部分组到一行,那么您可以再次GROUP_CONCAT()

评论后编辑:

SELECT
    therapist_name,
    GROUP_CONCAT( CONCAT(day, '(', locations, ')') SEPARATOR ',' ) AS locations
FROM (      
    SELECT
            t.therapist_name,
            dl.day,
            GROUP_CONCAT(DISTINCT dl.name SEPARATOR ',') AS locations
    FROM
            therapists t 
            LEFT JOIN days_location dl ON dl.therapist_id = t.id
            LEFT JOIN location l ON dl.location_id = l.id
    GROUP BY t.therapist_name, dl.day
    ) t
GROUP BY therapist_name

我还没有测试代码,所以可能会有一些小错误需要调整。无法在 atm 上对其进行测试。

关于MySQL group_concat with select inside select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35614678/

相关文章:

mysql - 缓存中的数据多久会老化并被驱逐出 MySQL - 查询运行缓慢然后快速

optimization - 优化我的 mysql 查询以使用索引进行排序

php - 数据库中的图像链接插入无需\

mysql - tomcat jdbc 故障转移主机

mysql - SQL 查询 - ORDER BY

php - 在 PHP 中创建一次性使用的安全 token 的最佳方法是什么?

mysql - 删除多列唯一键而不删除外键?

mysql - 不能在mysql workbench中使用datetime类型

mysql - 我如何知道何时应该优化我的查询?

php - 检查数组中有多少值大于 x