2016-01-17 62 views
-1

分割字符串中使用MSQL查询 -使用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查询。

+5

问题是:“为什么你在同一领域有多个数据?” – Toto

+0

可怕的数据库设计。我建议你开始通过转换您的数据库到3NF,然后使用普通查询,而不试图找出荒谬的解决方案 – Andrew

回答

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          | 
+------------------------------------------------+ 
+0

不正确的答案。 –

+0

你能告诉我我们怎么做,没有查询...我们可以做到这一点使用嵌套的foreach循环? –

+0

我不明白你为什么不想使用查询。将多个数据存储在一个字段中也不是最好的想法。所以我不能回答这个问题。我不擅长PHP。 –

0

可以使用波纹管查询所需outout,

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;

但它不是好的实践ce来创建这样的数据库设计。