我想獲得一份MySQL報告,但什麼也沒有。請指出有什麼不對?我檢查了這個相似的question,但無法找到解決我的問題。MySQL加入自己的表
表值
+----+-----+----------+---------+-----------+-----------+ | pk | ind | opt_type | name | opt_type1 | opt_type2 | +----+-----+----------+---------+-----------+-----------+ | 1 | 1 | group_1 | Green | - | - | | 2 | 1 | group_1 | Red | - | - | | 3 | 1 | group_2 | Cloth | - | - | | 4 | 1 | group_3 | Shirt | 1 | 3 | | 5 | 1 | group_4 | Table | 2 | - | +----+-----+----------+---------+-----------+-----------+
所需的輸出
name | colour | material ------+--------+--------- Shirt | Green | Cloth Table | Red |
QUERY & CODE
SELECT
o.name
, o1.name as colour
, o2.name as material
FROM $options as o
LEFT JOIN $options AS o1 ON o1.opt_type1 = o.pk
LEFT JOIN $options AS o2 ON o2.opt_type2 = o.pk
WHERE o.ind=$ind
AND o1.ind=$ind
AND o2.ind=$ind
LOOP{
echo $name ." ". $colour ." ". $material;
}
可以請您用'$ options'和'$ ind'顯示最終查詢替換爲實際值嗎? –
什麼是$ options – JYoThI
查看使用'GROUP_CONCAT' [鏈接](https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat) – Ryan