我有一个函数,它运行并从查找表中获取存储在特定表中的值的标签。当有一个值时,它会正确显示。但是,当有多个值时,它只返回第一个值。例如:
Lookup table is
| Tel_No| Tel_type |
--------------------
| 1 | Info |
| 2 | Support |
| 3 | Call |
Main table is
| TelephoneCalls |
------------------
| 1,3 |
| 3 |
| 1,2,3 |
我现在拥有的用于匹配1个值的函数是
function getMonitoring($monitoring){
$query = "SELECT Tel_type FROM TelephoneCalls Where Tel_no = '$monitoring'";
$result9 =mysql_query($query) or die(mysql_error());
list($Tel_type) = mysql_fetch_array($result9, MYSQL_NUM);
return $Tel_type;
}
我怎么能让它列出如下的值
If 1, 3 then display Info, Call
If 3 display Call
If 1, 2, 3 display Info, Support, Call
谢谢你的帮助!
我猜评论触及了它,但您确实应该将模式更改为更多的多对多关系,而不是在字段中使用CSV值。如果你不能,这个查询应该工作:
SELECT telephonecalls, GROUP_CONCAT(tel_type)
FROM lookup_table
LEFT JOIN main_table ON FIND_IN_SET(tel_no , replace(TelephoneCalls,' ', '')) > 0
GROUP BY telephonecalls;
#outputs
+----------------+------------------------+
| telephonecalls | GROUP_CONCAT(tel_type) |
+----------------+------------------------+
| 1,2,3 | Info,Support,Call |
| 1,3 | Info,Call |
| 3 | Call |
+----------------+------------------------+
http://sqlfiddle.com/!2/194fd/1/0