有可能更好地解决这个问题吗?
我有两个mysql表。
1) z_import_kategorie
id - kategorieHOME - kategoriePP - kategorieP
1 - 2 - 3 - 5
2 - 2 - 4 - 8
3 - 2 - 6 - 7
2) tm_category_lang
id_category - name - lang_id
2 - HOME - 1
2 - Uvod - 2
3 - Bikes - 1
3 - Kola - 2
4 - Windows - 1
4 - Okna - 2
5 - Cars - 1
5 - Auta - 2
6 - Wheels - 1
6 - Disky - 2
7 - Lights - 1
7 - Svetla - 2
8 - Doors - 1
8 - Dvere - 2
我需要将表z_import_kategorie中的kategorieHOME,kategoriePP和kategorieP加入到单个查询中的表tm_category_lang中的类别名称。
我对此的质询是:
SELECT z.*,k1.name AS katHOME,k2.name AS katPP,k3.name AS katP
FROM z_import_kategorie AS z
LEFT JOIN tm_category_lang AS k1 ON(k1.id_category = z.kategorieHOME)
LEFT JOIN tm_category_lang AS k2 ON(k2.id_category = z.kategoriePP)
LEFT JOIN tm_category_lang AS k3 ON(k3.id_category = z.kategorieP)
GROUP BY z.id
ORDER BY z.id ASC
RESULT of query is:
1 - HOME - Bikes - Cars
2 - HOME - Windows - Doors
3 - HOME - Wheels - Lights
可以更好地设计此查询吗?我的查询有效,但我认为这不好。
谢谢。
答案 0 :(得分:1)
您的查询中无需使用 GROUP BY 来获取类别名称,因此请将其删除,然后您的查询才能生效。
试试这个:
SELECT z.*, k1.name AS katHOME, k2.name AS katPP, k3.name AS katP
FROM z_import_kategorie AS z
LEFT JOIN tm_category_lang AS k1 ON k1.id_category = z.kategorieHOME AND k1.lang_id = 1
LEFT JOIN tm_category_lang AS k2 ON k2.id_category = z.kategoriePP AND k2.lang_id = 1
LEFT JOIN tm_category_lang AS k3 ON k3.id_category = z.kategorieP AND k3.lang_id = 1
ORDER BY z.id ASC