想要使用MySQL group_concat将两行合并为一行 [英] Want to Merge two rows into one using MySQL group_concat

查看:140
本文介绍了想要使用MySQL group_concat将两行合并为一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 group_concat mysql查询将两行合并为一个

  $ query = $ this-> db-> select('trt.day,group_concat(trt.open_time,trt.close_time)')
-> from('tbl_restaurant_timing as trt')
-> where('trt.restaurant_id',$ restaurant_id,FALSE)
-> group_by('trt.day')
-> get();

我尝试了上面的方法,但是它提供了其他类似下面的结果

 数组

[0] =>数组

[day] =>星期五
[group_concat(trt.open_time,trt.close_time)] => 18:0023:00,10:0015:00


[1] =>数组

[day] => Mon
[group_concat(trt.open_time,trt.close_time)] => 10:0015:00,18:0023:00


[2] =>数组

[day] =>星期六
[group_concat(trt.open_time,trt.close_time)] => ; 18:0023:00,10:0015:00


[3] =>数组

[day] => gt Sun
[group_concat(trt.open_time,trt.close_time)] => 10:0015:00,18:0023:00


[4] =>数组

[day] => Thu
[group_concat(trt.open_ti我,trt.close_time)] => 10:0015:00,18:0023:00


[5] =>数组

[day] => Tue
[group_concat(trt.open_time,trt.close_time)] => gt; 18:0023:00,10:0015:00


[6] =>阵列

[day] => Wed
[group_concat(trt.open_time,trt.close_time)] => gt; 18:0023:00,10:0015:00



在这里您可以看到第二个插槽排在第一一些行是错误的。





所以请检查我错了。谢谢

解决方案

请查找以下提到的解决方案,有点冗长的操作,但我认为这会为您提供帮助。


$ b

 选择
trt.id,
trt.day,
(SELECT CONCAT_WS('-',open_time,close_time )从tbl_restaurant_timing trt1处,在trt1.day = trt.day ORT BY trt1.id ASC LIMIT 1)作为first_slot,从tbl_restaurant_timing trt1处
(SELECT CONCAT_WS('-',open_time,close_time)在trt1.day = trt。 trt1.id DESC LIMIT 1)作为第二时隙
从tbl_restaurant_timing trt
在哪里trt.restaurant_id = 1
GROUP BY trt.day
ORDER BY ID;




表数据




  + ---- + ---------------- + ----------- + ----------- + ------------ + 
| id | restaurant_id |天开放时间| close_time |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 1 | 1 |星期一| 10:00 | 15:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 2 | 1 |星期一| 18:00 | 23:50 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 3 | 1 |星期二| 09:00 | 15:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 4 | 1 |星期二| 18:00 | 23:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 6 | 1 |星期三| 10:00 | 15:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 7 | 1 |星期三| 18:00 | 23:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 8 | 1 |星期四| 10:00 | 15:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 9 | 1 |星期四| 18:00 | 23:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 13 | 1 |星期五| 10:00 | 15:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 14 | 1 |星期五| 18:00 | 23:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 15 | 1 |周六| 10:00 | 15:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 16 | 1 |周六| 18:00 | 23:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 17 | 1 |周日| 10:00 | 15:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +
| 18 | 1 |周日| 18:00 | 23:00 |
+ ---- + ---------------- + ----------- + ----------- + ------------ +




结果




  + ---- + ---- ------- + ------------- + ------------- + 
| id |天first_slot | second_slot |
+ ---- + ----------- + ------------- ++ ------------- +
| 1 |星期一| 10:00-15:00 | 18:00-23:50 |
+ ---- + ----------- + ------------- ++ ------------- +
| 3 |星期二| 09:00-15:00 | 18:00-23:00 |
+ ---- + ----------- + ------------- ++ ------------- +
| 6 |星期三| 10:00-15:00 | 18:00-23:00 |
+ ---- + ----------- + ------------- ++ ------------- +
| 8 |星期四| 10:00-15:00 | 18:00-23:00 |
+ ---- + ----------- + ------------- ++ ------------- +
| 13 |星期五| 10:00-15:00 | 18:00-23:00 |
+ ---- + ----------- + ------------- ++ ------------- +
| 15 |周六| 10:00-15:00 | 18:00-23:00 |
+ ---- + ----------- + ------------- ++ ------------- +
| 17 |周日| 10:00-15:00 | 18:00-23:00 |
+ ---- + ----------- + ------------- ++ ------------- +

让我知道它是否无效。


i want to merge 2 rows as one using group_concat mysql query

$query = $this->db->select('trt.day, group_concat(trt.open_time, trt.close_time)')
                          ->from('tbl_restaurant_timing as trt')
                          ->where('trt.restaurant_id', $restaurant_id, FALSE)
                          ->group_by('trt.day')
                          ->get();

I have tried the above one but it provide some other result like below

Array
(
    [0] => Array
        (
            [day] => Fri
            [group_concat(trt.open_time, trt.close_time)] => 18:0023:00,10:0015:00
        )

    [1] => Array
        (
            [day] => Mon
            [group_concat(trt.open_time, trt.close_time)] => 10:0015:00,18:0023:00
        )

    [2] => Array
        (
            [day] => Sat
            [group_concat(trt.open_time, trt.close_time)] => 18:0023:00,10:0015:00
        )

    [3] => Array
        (
            [day] => Sun
            [group_concat(trt.open_time, trt.close_time)] => 10:0015:00,18:0023:00
        )

    [4] => Array
        (
            [day] => Thu
            [group_concat(trt.open_time, trt.close_time)] => 10:0015:00,18:0023:00
        )

    [5] => Array
        (
            [day] => Tue
            [group_concat(trt.open_time, trt.close_time)] => 18:0023:00,10:0015:00
        )

    [6] => Array
        (
            [day] => Wed
            [group_concat(trt.open_time, trt.close_time)] => 18:0023:00,10:0015:00
        )

)

Here you can see the 2nd slot comes first in some rows which is wrong.

This one my table i want result in php table like below

So Please check where i am wrong. Thank you

解决方案

Please find below mentioned solutions, Bit lengthy operation but I think this will help you.

SELECT 
    trt.id,
    trt.day, 
    (SELECT CONCAT_WS('-',open_time, close_time) from tbl_restaurant_timing trt1 WHERE trt1.day = trt.day ORDER BY trt1.id ASC LIMIT 1) as first_slot,
    (SELECT CONCAT_WS('-',open_time, close_time) from tbl_restaurant_timing trt1 WHERE trt1.day = trt.day ORDER BY trt1.id DESC LIMIT 1) as second_slot
FROM tbl_restaurant_timing trt
WHERE trt.restaurant_id = 1
GROUP BY trt.day
ORDER BY id;

Table Data

+----+----------------+-----------+-----------+------------+
| id | restaurant_id  | day       | open_time | close_time |
+----+----------------+-----------+-----------+------------+
| 1  | 1              | Monday    | 10:00     | 15:00      |
+----+----------------+-----------+-----------+------------+
| 2  | 1              | Monday    | 18:00     | 23:50      |
+----+----------------+-----------+-----------+------------+
| 3  | 1              | Tuesday   | 09:00     | 15:00      |
+----+----------------+-----------+-----------+------------+
| 4  | 1              | Tuesday   | 18:00     | 23:00      |
+----+----------------+-----------+-----------+------------+
| 6  | 1              | Wednesday | 10:00     | 15:00      |
+----+----------------+-----------+-----------+------------+
| 7  | 1              | Wednesday | 18:00     | 23:00      |
+----+----------------+-----------+-----------+------------+
| 8  | 1              | Thursday  | 10:00     | 15:00      |
+----+----------------+-----------+-----------+------------+
| 9  | 1              | Thursday  | 18:00     | 23:00      |
+----+----------------+-----------+-----------+------------+
| 13 | 1              | Friday    | 10:00     | 15:00      |
+----+----------------+-----------+-----------+------------+
| 14 | 1              | Friday    | 18:00     | 23:00      |
+----+----------------+-----------+-----------+------------+
| 15 | 1              | Saturday  | 10:00     | 15:00      |
+----+----------------+-----------+-----------+------------+
| 16 | 1              | Saturday  | 18:00     | 23:00      |
+----+----------------+-----------+-----------+------------+
| 17 | 1              | Sunday    | 10:00     | 15:00      |
+----+----------------+-----------+-----------+------------+
| 18 | 1              | Sunday    | 18:00     | 23:00      |
+----+----------------+-----------+-----------+------------+

Result

+----+-----------+-------------+-------------+
| id | day       | first_slot  | second_slot |
+----+-----------+-------------+-------------+
| 1  | Monday    | 10:00-15:00 | 18:00-23:50 |
+----+-----------+-------------+-------------+
| 3  | Tuesday   | 09:00-15:00 | 18:00-23:00 | 
+----+-----------+-------------+-------------+
| 6  | Wednesday | 10:00-15:00 | 18:00-23:00 |  
+----+-----------+-------------+-------------+
| 8  | Thursday  | 10:00-15:00 | 18:00-23:00 | 
+----+-----------+-------------+-------------+
| 13 | Friday    | 10:00-15:00 | 18:00-23:00 | 
+----+-----------+-------------+-------------+
| 15 | Saturday  | 10:00-15:00 | 18:00-23:00 |
+----+-----------+-------------+-------------+
| 17 | Sunday    | 10:00-15:00 | 18:00-23:00 |
+----+-----------+-------------+-------------+

Let me know if it not works.

这篇关于想要使用MySQL group_concat将两行合并为一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆