doudou130216 2012-11-22 17:39 采纳率: 0%
浏览 320
已采纳

MYSQL查询多个逗号分隔的字符串

I have data stored in MYSQL database as shown below:

 id    t_id     type    color       Y        S       M          L   
  2    2606      2    Black DNA   1,5,8    4,2,6    7,3,9    10,11,12   

I want the query to return like this with PHP:

Total:

Black DNA

1 (Y)  
2 (S)  
3 (M)  
4 (S)  
5 (Y)  
6 (S)  
7 (M)  
8 (Y)  
9 (M)  
10 (L)  
11 (L)  
12 (L)  

I am using Joomla 2.7, Here is what I have tried in PHP:

$query = "SELECT DISTINCT(e.id) as id, e.color, GROUP_CONCAT(e.S) as small FROM  #__bl_equipment as e WHERE e.type = 4 AND e.t_id = 2606";
       $db->setQuery($query);
    $equip1 = $db->loadObjectList();


<table>

<?php foreach($this->equip1 as $equip){

echo '<tr><td>';
echo $equip->color;
echo '</td><td>';
echo $equip->small;
echo '</td></tr>';}

?>


</table>

Only result I have been able to get:

Black DNA 1, 2, 3

  • 写回答

1条回答 默认 最新

  • dongpeng7744 2012-11-22 17:44
    关注

    That would be too complicated with SQL (and doubt it's possible even)

    I'd do the following - Get Y, S, M and L columns - Parse each using commas (so you get numbers) and turn them into arrays - Sort each

    So now you should have something like

    y_numbers = [1,5,8]

    s_numbers = [2,4,6]

    m_numbers = [3,7,9]

    l_numbers = [10,11,12]

    Then inside a loop, look at first element of each array, find the smallest, print letter based on which array it is, iterate the pointer for that array... continue until you've exhausted all numbers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号