我有一些價值觀連字符分隔的一排:連字符分隔的值和LEFT_JOIN
表: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>';
}
...
EDIT2:
這是相當我很難解釋英語我的問題,但我想盡我所能:)
表中的「live_customers」我確實有這樣的:
id | areas
1 | 10-20-30
2 | 40-50-60
...
表中的「區域」
(這是一個完全不同的表):
id | value
38 | Zone1
39 | Zone2
40 | Zone3
...
在你看到的只是SQL查詢表變量,因爲我previousvly宣佈他們在頁面的頂部:
$table5 = 'areas';
$dblist = 'live_customers';
等。
解決方案
感謝任何人的答案和讓我知道「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
結果是我所期待的^^
最有可能使用'GROUP_CONCAT()'。請發佈其餘的查詢,我們可以幫助您加入。 – 2013-03-11 14:11:32
感謝您的回答,只是對其進行了編輯。 – Imero 2013-03-11 14:17:47
我沒有看到有類似'areas'的任何列。你想列出哪一列?這將有助於查看實際的表和列,而不是PHP變量。 – 2013-03-11 14:25:07