1

我对这种关系类型的数据库设计很陌生。我只是以这种方式设计了数据库。但是,我JOIN对 MySQL 感到很困惑。加入所有这张表的查询应该是什么。如果你能看到表格users是所有表格的参考。

用户

+----------+----------------+-----------------+
| users_id | users_level_id | users_status_id |
+----------+----------------+-----------------+
|        1 |              1 |               1 |
|        2 |              2 |               1 |
+----------+----------------+-----------------+

users_credentials

+----------+---------------------------+-----------------------------+----------------------------+
| users_id | users_credential_username | users_credential_email      | users_credential_password  |
+----------+---------------------------+-----------------------------+----------------------------+
|        1 | super                     | super@gmail.com             | $5$e94e9e$vptscyHjm8rdX0j6 |
|        2 | admin                     | admin@gmail.com             | $5$fVuOmySyC0PttbiMn8in0k7 |
+----------+---------------------------+-----------------------------+----------------------------+

用户级别

+----------------+-------------------------+
| users_level_id | users_level_description |
+----------------+-------------------------+
|              1 | Super Administrator     |
|              2 | Administrator           |
+----------------+-------------------------+

用户状态

+-----------------+--------------------------+
| users_status_id | users_status_description |
+-----------------+--------------------------+
|               0 | Disabled                 |
|               1 | Enabled                  |
+-----------------+--------------------------+
4

5 回答 5

2

试试这个

SELECT u.*, uc.*, ul.*, us.*
FROM users u 
INNER JOIN users_credentials uc
    ON u.users_id = uc.users_id
INNER JOIN users_level ul
    ON u.users_level_id = ul.users_level_id
INNER JOIN users_status us
    ON u.users_status_id = us.users_status_id

注意使用INNER JOIN:这意味着如果用户在连接表上没有相应的记录,则不会显示;如果您需要返回每个用户,即使相关表上没有匹配记录,请更改INNER JOINLEFT JOIN.

用户评论后编辑:
如果您只想返回某些列,请将其定义为本示例

SELECT uc.users_credential_username AS username, 
       uc.users_credential_email AS email, 
       uc.users_credential_password AS pwd, 
       ul.users_level_description AS level, 
       us.users_status_description AS status
于 2012-04-13T06:03:36.487 回答
1

我认为这看起来像这样:

SELECT * FROM users
LEFT JOIN user_credentials ON users.user_id = user_credential.user_id
LEFT JOIN user_level ON users.users_level_id = users_level.users_level_id

等等..

于 2012-04-13T06:04:47.557 回答
1

这是一个简单的查询,它将加入所有这些

select *
from users
  left join users_credentials
    on users_credentials.users_id = users.users_id
  left join users_level
    on users_level.users_level_id = users.users_level_id
  left join users_status
    on users_status.users_status_id = users.users_status_id

编辑

如果你想从不同的表中获取数据用户这个

select users.* , users_credentials.* , users_level.* , users_status.*
from users
  left join users_credentials
    on users_credentials.users_id = users.users_id
  left join users_level
    on users_level.users_level_id = users.users_level_id
  left join users_status
    on users_status.users_status_id = users.users_status_id
于 2012-04-13T06:06:10.817 回答
1

使用这种类型的查询......

SELECT c.*, l.*, s.*
FROM users AS u
INNER JOIN users_credentials AS c ON (u.users_id = C.users_id)
INNER JOIN users_level AS l ON (u.users_level_id= l.users_level_id)
INNER JOIN users_status AS s ON (u.users_status_id= s.users_status_id)

您可以在哪里指定您想要的字段。* ...

于 2012-04-13T06:07:41.793 回答
1

连接用于从与引用表有外键关系的规范化表中获取数据。对于上面带有连接的表,您可以借助引用表在两个表之间获取数据。例如

Select * from users a JOIN users_credentials b 
ON a.user_id=b.user_id JOIN users_level c 
ON c.users_level_id=a.users_level_id 
where users_credential_username='super';

此查询的结果将为您提供有关 users_credential_username=super 的用户的详细信息,例如 users_level_description。

于 2012-04-13T06:21:43.950 回答