简体   繁体   English

如何加入很多桌子?

[英]How to join many tables ?

I am very confused on how JOIN tables work. 我对JOIN表的工作方式感到很困惑。 I tried to read every possible documentation without getting anything good. 我试图阅读所有可能的文档而没有得到任何好处。

I am trying to do a big SELECT on various tables to get all the data of a specific user. 我试图在各种表上做一个大的SELECT来获取特定用户的所有数据。 The below query seems to loop forever without any good result. 以下查询似乎永远循环,没有任何好结果。

Can anyone explain in the easiest way to do JOIN in a correct way? 任何人都可以用最简单的方式解释以正确的方式进行JOIN吗? Thanks 谢谢

SELECT user.id_user, name, surname, email, password, status, phone, address, city, zip_code, company_name, cf, piva, public, icon, description, note, web, facebook_id, facebook_token, twitter_id, twitter_token, linkedin_id, linkedin_token, ip, date, link, confirmed, newsletter, legal_validation FROM user,user_addfields_anagraphic,user_addfields_legal,user_addfields_public,user_addfields_social,user_registration WHERE user.id_user = '43534534534'

You aren't joining the tables together in any meaningful way, so you end up with a Cartesian Product, which is where every row is joined with every other row. 您没有以任何有意义的方式将表连接在一起,因此您最终会得到一个笛卡尔积,这是每一行与其他行连接的地方。

you have to find the columns in each table that allow then to relate to each other ( it's a relational database we're dealing with ). 你必须在每个表中找到允许彼此相关的列(它是我们正在处理的关系数据库)。

So if you want to know which employees are in a department, you would do something like 因此,如果您想知道某个部门的员工,您会做类似的事情

select name, department 
from employee emp join Department d 
on emp.dept_no = d.dept_no
where dept.description = "SALES"

the key thing to recognise is the join condition - it specifies how you relate the contents of one table to the other. 要识别的关键是连接条件 - 它指定了如何将一个表的内容与另一个表的内容相关联。

Your statement is quite complex - I'd recommend breaking it down int much smaller chunks. 你的陈述非常复杂 - 我建议把它分解成更小的块。 Get a two table join to work and then gradually add the other tables in. 获取两个表连接工作,然后逐步添加其他表。

Try like 试试吧

   SELECT table1.* , 
   table2.* , 
   table3.* 
   FROM table1 
   JOIN table2 ON .... 
   JOIN table3 ON....

You need to join your other tables. 您需要加入其他表格。 I can´t find this in your where clause 我在你的where子句中找不到这个

You could do something like this: 你可以这样做:

SELECT user.id_user, name, surname, email, password, status, phone, address, city, zip_code, company_name, cf, piva, public, icon, description, note, web, facebook_id, facebook_token, twitter_id, twitter_token, linkedin_id, linkedin_token, ip, date, link, confirmed, newsletter, legal_validation
FROM user
INNER JOIN user_addfields_anagraphic on user.user_id = user_addfields_anagraphic.user_id
...
WHERE user.id_user = '43534534534'

Or like this (which is the same) 或者像这样(这是相同的)

SELECT user.id_user, name, surname, email, password, status, phone, address, city, zip_code, company_name, cf, piva, public, icon, description, note, web, facebook_id, facebook_token, twitter_id, twitter_token, linkedin_id, linkedin_token, ip, date, link, confirmed, newsletter, legal_validation
FROM user, user_addfields_anagraphic, ...
WHERE user.id_user = '43534534534' and
user.user_id = user_addfields_anagraphic.user_id and
...

In this example above I guess, that user_addfields_anagraphic.user_id is your foreign key to your primary key in table user called "user_id". 在上面的这个例子中,我猜,user_addfields_anagraphic.user_id是表用户中名为“user_id”的主键的外键。

You need also to join the other tables with this syntax as shown above. 您还需要使用此语法连接其他表,如上所示。 But trying to select from multiple tables wihtout a where clause where to join the other tables is impossible. 但是尝试从多个表中选择没有where子句的地方是不可能的。

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

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