简体   繁体   English

在 MySQL 中加入多个不同的表

[英]Joining multiple different tables in MySQL

Example to join 4 or more tables连接 4 个或更多表的示例

t1 - f1,f2,f3
t2 - f1,f4,f5
t3 - f4,f6,f7,f8
t4 - f8,f9

t1,t2,t3,t4 are 4 tables and f... are corresponding fields. t1,t2,t3,t4 是 4 个表,f... 是对应的字段。 Please suggest a join query which is optimized for computation and memory.请建议一个针对计算和内存进行优化的连接查询。 Eg;例如; I want to know how many f2 X f4 X f7 X f8.我想知道有多少 f2 X f4 X f7 X f8。

Does the order of joining tables and size of tables have any impact on sql syntax?加入表的顺序和表的大小对sql语法有影响吗?

I searched through multiple forums and somewhere inner join is being used and UNION and somewhere sub queries.我搜索了多个论坛,并在某个地方使用了内部连接以及 UNION 和某个地方的子查询。

Please explain with syntax of MySQL.请解释MySQL的语法。

Just write all the joins one after the other.只需一个接一个地编写所有连接。

SELECT COUNT(*)
FROM t1
JOIN t2 ON t1.f1 = t2.f1
JOIN t3 ON t2.f4 = t3.f4
JOIN t4 ON t3.f8 = t4.f8

The order of the joins shouldn't matter.连接的顺序应该无关紧要。 Make sure you have indexes on all the columns being used in the ON conditions.确保在ON条件中使用的所有列上都有索引。

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

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