MySQL:基于用户连接四个表
昨天花了几个小时之后,我决定在这里发帖。我有一个只有几个表的数据库,我需要根据users表的idusers(user的id)连接4个表 需要联接的四个表是:用户、表1、表2和表3MySQL:基于用户连接四个表,mysql,sql,join,left-join,Mysql,Sql,Join,Left Join,昨天花了几个小时之后,我决定在这里发帖。我有一个只有几个表的数据库,我需要根据users表的idusers(user的id)连接4个表 需要联接的四个表是:用户、表1、表2和表3 users idusers, name, email, a, b, c, etc. table1 custom_id1, a_id, users_idusers table2 custom_id2, a_id, users_idusers, comment table3 custom_id3, lang
users
idusers, name, email, a, b, c, etc.
table1
custom_id1, a_id, users_idusers
table2
custom_id2, a_id, users_idusers, comment
table3
custom_id3, lang
它们之间的关系:
-用户注册并将数据存储到users表:我需要该表中的所有字段。
-当他们采取行动时,它会存储在表1中(其中users\u idusers是users.idusers)。
-一些操作(带有注释)注册到表2(users\u idusers是users.idusers)。
-表1和表2的a_id字段相同,并映射到表3的自定义_id 3
我需要什么:
我希望能够从用户表、lang的表3和表2(如果有注释,获取文本或返回该字段的null)中获取表1中映射到其用户的所有操作
我的查询是什么样子的:
SELECT table1.a_id, table1.users_idusers, users.*, table3.langkey
FROM table1
LEFT JOIN users ON (users.idusers = table1.users_idusers)
LEFT JOIN table3 ON (table1.a_id = table3.custom_id3);
上面的查询缺少需要加入表2的部分,这就是我遇到的问题。任何帮助都将不胜感激
谢谢。你能发布你的表格的完整结构吗?你不能像你把
table1
加入users
那样加入table2
吗?@PatrickQ假设你是指table3而不是table1。这就是我昨天一直在为表2做的事情。由于某种原因,我的15K结果变成700K。所以不,做另一个连接是不起作用的。我认为这不起作用。你能再检查一下吗?谢谢
SELECT u.*
, and
, some
, other
, columns
FROM users u
JOIN table1 a
ON a.users_idusers = u.idusers
JOIN table3 l
ON l.custom_id3 = a.a_id
LEFT
JOIN table2 c
ON c.a_id = a.a_id
[AND c.users_idusers = a.users_idusers]??