我有一排用连字符分隔的一些值:
表:live_customers
行:区域
id | areas
1 | 10-20-30
2 | 40-50-60
...
使用这个...
LEFT JOIN $table5 AS table5 ON live.areas REGEXP CONCAT('(^|-) ?',table5.id,' ?($|-)')
我的结果看起来像:
(tab id:1) area: 10
(tab id:1) area: 20
...
(tab id:2) area: 40
...
但我希望:
(tab id:1) area: 10,20,30
(tab id:2) area: 40,50,60
我怎么能解决这个问题?
编辑:
完整的查询如下所示:
SELECT live.*,
live.id AS lid,
table1.id, table1.value AS tn_val,
table2.id, table2.value AS tp_val,
table3.id, table3.value AS ht_val,
table5.id, table5.value AS ar_val
FROM $dblist AS live
LEFT JOIN $table1 AS table1 ON live.town = table1.id
LEFT JOIN $table2 AS table2 ON live.htype = table2.id
LEFT JOIN $table3 AS table3 ON live.ht = table3.id
LEFT JOIN $table5 AS table5 ON live.areas REGEXP CONCAT('(^|-) ?',table5.id,' ?($|-)')
ORDER BY live.id ASC
PHP 回声:
...
if ($post['areas']){ // Debugging areas stuff
echo '<strong>'.$_areas.': (ar_val)</strong> '.$post['ar_val'].'<p>';
echo '<strong>'.$_areas.': (areas)</strong> '.$post['areas'].'<p>';
}
...
编辑2:
我很难用英语解释我的问题,但我正在尽我所能:)
在“live_customers”表中,我确实有这个:
id | areas
1 | 10-20-30
2 | 40-50-60
...
在“区域”表(这是一个完全不同的表)中:
id | value
38 | Zone1
39 | Zone2
40 | Zone3
...
在 SQL 查询中,您只看到表变量,因为我之前在页面顶部声明了它们:
$table5 = 'areas';
$dblist = 'live_customers';
ETC..
解决方案
感谢任何人的回答并让我知道“GROUP_CONCAT”。
这是我的解决方案:
SELECT live.*,
live.id AS lid,
table1.id, table1.value AS tn_val,
table2.id, table2.value AS tp_val,
table3.id, table3.value AS ht_val,
table5.id, GROUP_CONCAT(table5.value) AS ar_val
FROM $dblist AS live
LEFT JOIN $table1 AS table1 ON live.town = table1.id
LEFT JOIN $table2 AS table2 ON live.htype = table2.id
LEFT JOIN $table3 AS table3 ON live.ht = table3.id
LEFT JOIN $table5 AS table5 ON FIND_IN_SET(table5.id, REPLACE(live.areas, '-', ','))
GROUP BY live.id
结果如我所愿^^