简体   繁体   中英

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.

I have basically two tables in one database. Table member_privileges 1 keeps users reputation, and table 2 keeps user details. 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.

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. So, if in member_privileges a username "john" has a can_edit_articles set to 1, then that user can edit.

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. ...

Use an inner join and prevent your sql statements from sql injection

If you would like to select the id, username and can_edit_articles values:

"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. 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')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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