Php 使用MYSQL查询拆分字符串?

Php 使用MYSQL查询拆分字符串?,php,mysql,Php,Mysql,使用MYSQL查询拆分字符串- id message mobile status 1 hello 9074739352 DELIVERED 2 hi 9074739352,9074739353 DELIVERED,FAILED 3 Testing 90

使用MYSQL查询拆分字符串-

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
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'
            )
)
使用此查询-从发送短信中选择*

结果是这样的-

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查询得到这样的结果-

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
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'
            )
)
我想用MYSQL查询来解决这个问题。

您可以使用子字符串索引来解决这个问题

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


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

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;

但是,创建这样的数据库设计不是一个好的做法。

问题是:为什么在同一个字段中有多个数据?糟糕的数据库设计。我建议您首先将数据库转换为3NF,然后使用普通查询,而不尝试找出荒谬的解决方案。您能否告诉我,在没有查询的情况下,我们如何做到这一点?我们可以使用嵌套的foreach循环吗?我不明白您为什么不想使用查询。在一个字段中存储多个数据也不是最好的主意。所以我不能回答这个问题。我不擅长php。