我有一个数据库表cranedetails(字段为:cf_did , cf_firstname, cf_cranetype
)
cf_did cf_firstname cf_cranetype
1 Alexy 2,3
2 Thomas 11,6,3
3 Thomas 5,6,11,3
4 Thomasxc 1,6,9,4
5 Thomaseg 11,12,3
我有另一个表格类型(cf_did,cf_cname
)
cf_ctid cf_ctname
1 MOBILE CRANES
2 ROUGH TERRAIN CRANES
3 ALL TERRAIN CRANES
4 CRAWLER CRANES
5 YARD CRANES
6 ARICULATING BOOM CRANES
7 TRUCK CRANES: ARTICULATING BOOM
8 TRUCK CRANES: LATTICE BOOM
9 TRUCK CRANES: TELESCOPIC BOOM
10 TRUCK LOADER CRANES
11 ELEVATORS
12 ELEVATOR – HYDRAULIC
我想显示这样的值, 例如:
id: name: cranetype
1 Alexy ROUGH TERRAIN CRANES,ALL TERRAIN CRANES
2 Thomas ELEVATORS,ARICULATING BOOM CRANES,ALL TERRAIN CRANES
等等。
如何从表格cf_cranetype
2,3
(粗糙地形起重机,所有地形起重机)获取表起重机字段cranetype
值{(1}})字段。
我是教派$ did = 1。并使用此代码
cf_ctnamefield
但输出是:
$sql=mysql_query("SELECT * FROM `cf_directory` WHERE `cf_did` = '$did'",$con);
$row = mysql_fetch_array($sql);$cn= $row['11'];
$sql2=mysql_query("SELECT * FROM `cf_cranetype` WHERE `cf_ctid`= '$cn'",$con);
<?php while($row2 = mysql_fetch_array($sql2)){
$cranename=$row2['cf_ctname']; ?>
<?php echo $cranename;?>
我想:
id: name: cranetype
1 Alexy ROUGH TERRAIN CRANES
答案 0 :(得分:0)
您只需GROUP_CONCAT()
与FIND_IN_SET()
一起使用即可:
SELECT
cd.cf_did AS id,
cd.cf_firstname AS name,
GROUP_CONCAT(ct.cf_ctname SEPARATOR ', ') AS cranetype
FROM
cranedetails cd,
cranetype ct
WHERE
FIND_IN_SET(ct.cf_ctid, cd.cf_cranetype)
GROUP BY
cd.cf_did,
cd.cf_firstname
输出是:
+----+----------+---------------------------------------------------------------------------------------+
| id | name | cranetype |
+----+----------+---------------------------------------------------------------------------------------+
| 1 | Alexy | ROUGH TERRAIN CRANES, ALL TERRAIN CRANES |
| 2 | Thomas | ARICULATING BOOM CRANES, ELEVATORS, ALL TERRAIN CRANES |
| 3 | Thomas | ALL TERRAIN CRANES, YARD CRANES, ARICULATING BOOM CRANES, ELEVATORS |
| 4 | Thomasxc | CRAWLER CRANES, ARICULATING BOOM CRANES, TRUCK CRANES: TELESCOPIC BOOM, MOBILE CRANES |
| 5 | Thomaseg | ALL TERRAIN CRANES, ELEVATORS, ELEVATOR – HYDRAULIC |
+----+----------+---------------------------------------------------------------------------------------+
5 rows in set
如果您只想按名称对结果进行分组(因为存在重复的名称),只需从查询中删除cd.cf_did
并添加DISTINCT
即可过滤重复的cranetype
值,即:
SELECT
cd.cf_firstname AS name,
GROUP_CONCAT(DISTINCT ct.cf_ctname SEPARATOR ', ') AS cranetype
FROM
cranedetails cd,
cranetype ct
WHERE
FIND_IN_SET(ct.cf_ctid, cd.cf_cranetype)
GROUP BY
cd.cf_firstname
结果将是:
+----------+---------------------------------------------------------------------------------------+
| name | cranetype |
+----------+---------------------------------------------------------------------------------------+
| Alexy | ROUGH TERRAIN CRANES, ALL TERRAIN CRANES |
| Thomas | ELEVATORS, ALL TERRAIN CRANES, YARD CRANES, ARICULATING BOOM CRANES |
| Thomaseg | ELEVATORS, ELEVATOR – HYDRAULIC, ALL TERRAIN CRANES |
| Thomasxc | TRUCK CRANES: TELESCOPIC BOOM, MOBILE CRANES, CRAWLER CRANES, ARICULATING BOOM CRANES |
+----------+---------------------------------------------------------------------------------------+
4 rows in set
答案 1 :(得分:-1)
答案 2 :(得分:-1)
对我来说,看起来你正在存储一个由逗号连接的整数值(你的cranetype id)的字符串值(或VARCHAR)。
如果是这样,我会考虑重新设计你的数据库,以便有一个表格,例如
在起重机细节中,为与起重机名称相关的每种起重机类型输入一个条目。 cf_nid和cf_ctid当然应该是与各个表的外键关系。
使用您当前的设计,您必须爆炸cf_cranetype的字符串值以获取整数数组(或字符串,看起来像整数),并且您的第二个查询必须如下所示:
"SELECT * FROM cf_cranetype WHERE cf_ctid IN '" . join($YOUR_ARRAY, ',') . "'"