使用MSQL查询拆分字符串?

时间:2016-01-17 12:45:51

标签: php mysql

使用MSQL查询分割字符串 -

id      message       mobile                                status      
1       hello       9074739352                          DELIVERED
2       hi          9074739352,9074739353               DELIVERED,FAILED
3       Testing     9074739353                          DELIVERED
4       Sorav       9074739353,9074739354,9074739355    DELIVERED,FAILED,DELIVERED
5       good        9074739353                          DELIVERED

使用此查询 - SELECT * FROM send_sms

它得到的结果 -

array(
    [0] => array(
            'id' => 1,
            'message' => 'hello',
            'mobile'  => '9074739352',
            'status'  => 'DELIVERED'
            ),
    [1] => array(
            'id' => 2,
            'message' => 'hi',
            'mobile'  => '9074739352,9074739353',
            'status'  => 'DELIVERED,FAILED'
            ),
    [2] => array(
            'id' => 3,
            'message' => 'Testing',
            'mobile'  => '9074739353',
            'status'  => 'DELIVERED'
            ),
    [3] => array(
            'id' => 4,
            'message' => 'Sorav',
            'mobile'  => '9074739353,9074739354,9074739355',
            'status'  => 'DELIVERED,FAILED,DELIVERED'
            ),
    [4] => array(
            'id' => 5,
            'message' => 'good',
            'mobile'  => '9074739353',
            'status'  => 'DELIVERED'
            )
)

但我希望使用MYSQL查询 -

array(
    [0] => array(
            'id' => 1,
            'message' => 'hello',
            'mobile'  => '9074739352',
            'status'  => 'DELIVERED'
            ),
    [1] => array(
            'id' => 2,
            'message' => 'hi',
            'mobile'  => '9074739352',
            'status'  => 'DELIVERED'
            ),
    [2] => array(
            'id' => 2,
            'message' => 'hi',
            'mobile'  => '9074739353',
            'status'  => 'FAILED'
            ),
    [3] => array(
            'id' => 3,
            'message' => 'Testing',
            'mobile'  => '9074739353',
            'status'  => 'DELIVERED'
            ),
    [4] => array(
            'id' => 4,
            'message' => 'Sorav',
            'mobile'  => '9074739353',
            'status'  => 'DELIVERED'
            ),
    [5] => array(
            'id' => 4,
            'message' => 'Sorav',
            'mobile'  => '9074739354',
            'status'  => 'FAILED'
            ),
    [6] => array(
            'id' => 4,
            'message' => 'Sorav',
            'mobile'  => '9074739355',
            'status'  => 'DELIVERED'
            ),
    [7] => array(
            'id' => 5,
            'message' => 'good',
            'mobile'  => '9074739353',
            'status'  => 'DELIVERED'
            )
)

我想使用MSQL查询来解决这个问题。

2 个答案:

答案 0 :(得分:0)

您可以使用 SUBSTRING_INDEX 来执行此操作

SELECT
  id,
  message,
  SUBSTRING_INDEX(mobile,',',1) AS mobile,
  status
FROM send_sms;

<强>示例

MariaDB > select SUBSTRING_INDEX('9074739352,9074739353',',',1);
+------------------------------------------------+
| SUBSTRING_INDEX('9074739352,9074739353',',',1) |
+------------------------------------------------+
| 9074739352                                     |
+------------------------------------------------+

答案 1 :(得分:0)

您可以使用以下方式查询所需的输出

select id,message,SUBSTRING_INDEX(mobile,",",1) as mobile,SUBSTRING_INDEX(status,",",1) as status from send_sms
union select id,message,substring_index(SUBSTRING_INDEX(mobile,",",2),',',-1) as mobile,substring_index(SUBSTRING_INDEX(status,",",2),',',-1) as status from send_sms
union select id,message,substring_index(SUBSTRING_INDEX(mobile,",",3),',',-1) as mobile,substring_index(SUBSTRING_INDEX(status,",",3),',',-1) as status from send_sms;

但是创建这样的数据库设计并不是一个好习惯。