MySQL 多个值连接到一个 table - 更好的查询?

MySQL multiple values join to one table - better query?

有没有可能更好地解决这个任务?

我有两个 mysql tables.

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

我需要在单个查询中将 table z_import_kategorie 中的 kategorieHOME、kategoriePP 和 kategorieP 连接到 table 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

是否可以将此查询设计得更好?我的查询有效,但我认为这不太好。

谢谢。

无需在您的查询中使用 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