简体   繁体   English

mysql在一条语句中从两个表中选择两列

[英]mysql selecting two columns from two tables in one statement

I have been looking if an this has been answered before in SO, but I could not find any. 我一直在寻找是否已经在SO中得到了解答,但是我找不到任何答案。

I have basically two tables in one database. 我基本上在一个数据库中有两个表。 Table member_privileges 1 keeps users reputation, and table 2 keeps user details. member_privileges 1保留用户信誉,表2保留用户详细信息。 They look roughly like this. 他们看起来像这样。

mysql> describe member_privileges; 
   +---------------------------+--------------+------+-----+---------+----------------+
        | Field                     | Type         | Null | Key | Default | Extra          |
        +---------------------------+--------------+------+-----+---------+----------------+
        | id                        | int(11)      | NO   | PRI | NULL    | auto_increment |
        | username                  | varchar(255) | YES  |     | NULL    |                |
        | can_comment               | int(11)      | NO   |     | NULL    |                |
        | can_create_articles       | int(11)      | NO   |     | NULL    |                |
        | can_edit_articles         | int(11)      | YES  |     | NULL    |                |
        | can_edit_timeline         | int(11)      | NO   |     | NULL    |                |
        | can_remove_comments       | int(11)      | NO   |     | NULL    |                |
        | can_upload_profile_images | int(11)      | NO   |     | NULL    |                |
        | can_upload_article_images | int(11)      | NO   |     | NULL    |                |
        | can_approve_new_articles  | int(11)      | NO   |     | NULL    |                |
        | can_freez_articles        | int(11)      | NO   |     | NULL    |                |
        +---------------------------+--------------+------+-----+---------+----------------+

and this is the users table. 这是users表。

mysql> describe users;
+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| username       | varchar(255) | NO   |     | NULL    |                |
| password       | varchar(255) | NO   |     | NULL    |                |
| age            | int(11)      | NO   |     | NULL    |                |
| country        | varchar(255) | NO   |     | NULL    |                |
| email          | varchar(255) | NO   |     | NULL    |                |
| link           | varchar(255) | NO   |     | NULL    |                |
| reg_date       | date         | YES  |     | NULL    |                |
| ip             | varchar(255) | YES  |     | NULL    |                |
| gender         | varchar(10)  | YES  |     | NULL    |                |
| about          | varchar(255) | YES  |     | NULL    |                |
| is_activated   | int(11)      | YES  |     | NULL    |                |
| activation_key | varchar(255) | YES  |     | NULL    |                |
| image_path     | varchar(255) | YES  |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

So, in one page for example, I am trying to check if user 'john' has a can_edit_articles permission set. 因此,例如在一页中,我试图检查用户'john'是否设置了can_edit_articles权限。 So, if in member_privileges a username "john" has a can_edit_articles set to 1, then that user can edit. 因此,如果在member_privilegesusername “ john”的can_edit_articles设置为1,则该用户可以进行编辑。

The problem is checking those details at one. 问题是一并检查那些细节。 Unlike running two queries like I am doing it now. 不同于像我现在那样执行两个查询。

$email = $_SESSION['email']; 

$get_user = "SELECT id, username FROM members WHERE email = $email; 

$can_user_edit = "SELECT can_edit_articles FROM member_privileges WHERE $email = ?;

So, as you can see I am running two queries, and I would like to know if there is any way to run those within one mysql statement. 因此,如您所见,我正在运行两个查询,并且我想知道是否可以在一个mysql语句中运行那些查询。 ... ...

Use an inner join and prevent your sql statements from sql injection 使用内部联接并防止SQL语句被SQL注入

If you would like to select the id, username and can_edit_articles values: 如果要选择id,用户名和can_edit_articles值:

"SELECT m.id, m.username, p.can_edit_articles  
 FROM members m inner join member_privileges p  
 ON m.username=p.username WHERE m.email = '$email'"

You should use JOIN in your query. 您应该在查询中使用JOIN。 Like this: 像这样:

SELECT can_edit_articles FROM member_privileges 
P LEFT JOIN  members M ON P.username = M.username WHERE email = '$email' 
$email = $_SESSION['email']; 

$can_user_edit = "SELECT can_edit_articles FROM member_privileges  
                  WHERE username = (SELECT username FROM users WHERE email = '$email')";

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

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