简体   繁体   English

Mysql,如何避免同一个表上的多个连接

[英]Mysql, how to avoid multiple joins on the same table

Is there a way (using some mysql programming) to avoid multiple joins on the same table with different alias?有没有办法(使用一些 mysql 编程)来避免在同一个表上使用不同的别名进行多个连接? For example I have例如我有

SELECT *
FROM `container` AS `cont_link`  
LEFT JOIN `custom_field_string` AS `sf0` ON cont_link.id = sf0.container_id AND sf0.custom_field_id=60 
LEFT JOIN `custom_field_string` AS `sf3` ON cont_link.id = sf3.container_id AND sf3.custom_field_id=321 
LEFT JOIN `custom_field_string` AS `sf4` ON cont_link.id = sf4.container_id AND sf4.custom_field_id=322
LEFT JOIN `custom_field_int` AS `sf1` ON cont_link.id = sf1.container_id AND sf1.custom_field_id=319 
LEFT JOIN `custom_field_int` AS `sf2` ON cont_link.id = sf2.container_id AND sf2.custom_field_id=320  

The Join with custom_field_string table is done 3 times, because it has different attributes. Join with custom_field_string 表做了 3 次,因为它有不同的属性。 How can I do this just with 1 join but mantaining the different attributes.我怎么能只用 1 个加入但保持不同的属性来做到这一点。 I have the problem of reaching the 61 joins limit.我遇到了达到 61 个连接限制的问题。

SELECT *
FROM `container` AS `cont_link`  
LEFT JOIN `custom_field_string` AS `sf0` ON cont_link.id = sf0.container_id AND sf0.custom_field_id in (60, 321, 322);

If only three rows matched, you'd get 3 results here instead of 1 row with 3 columns.如果只有三行匹配,您会在这里得到 3 个结果,而不是 1 行 3 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM