简体   繁体   English

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

[英]MYSQL query multiple comma separated strings

I have data stored in MYSQL database as shown below:我将数据存储在 MYSQL 数据库中,如下所示:

 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:我希望查询像这样用 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:我正在使用 Joomla 2.7,这是我在 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黑色 DNA 1, 2, 3

That would be too complicated with SQL (and doubt it's possible even)这对 SQL 来说太复杂了(甚至怀疑它是可能的)

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我会执行以下操作 - 获取 Y、S、M 和 L 列 - 使用逗号解析每个列(这样你就会得到数字)并将它们变成数组 - 对每个列进行排序

So now you should have something like所以现在你应该有类似的东西

y_numbers = [1,5,8] y_numbers = [1,5,8]

s_numbers = [2,4,6] s_numbers = [2,4,6]

m_numbers = [3,7,9] m_numbers = [3,7,9]

l_numbers = [10,11,12] 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然后在循环内,查看每个数组的第一个元素,找到最小的,根据它是哪个数组打印字母,迭代该数组的指针......继续直到你用完所有数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM