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

时间:2015-01-25 02:44:09

标签: php mysql sql

我一直在寻找是否已经在SO中回答了这个问题,但我找不到任何问题。

我在一个数据库中基本上有两个表。表member_privileges 1保持用户信誉,表2保留用户详细信息。它们看起来大致如此。

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

这是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    |                |
+----------------+--------------+------+-----+---------+----------------+

因此,在一个页面中,我尝试检查用户'john'是否设置了can_edit_articles权限。因此,如果在member_privileges username"约翰"将can_edit_articles设置为1,然后该用户就可以进行编辑。

问题在于检查这些细节。不像我现在这样运行两个查询。

$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 = ?;

因此,正如您所看到的,我正在运行两个查询,并且我想知道是否有任何方法可以在一个mysql语句中运行它们。 ......

3 个答案:

答案 0 :(得分:3)

使用内部联接并防止sql注入的SQL语句

如果您想选择id,username和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'"

答案 1 :(得分:1)

您应该在查询中使用JOIN。像这样:

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

答案 2 :(得分:1)

$email = $_SESSION['email']; 

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