将2个字段连接到另一个表

时间:2019-01-16 09:31:35

标签: php mysql

如何将表1中的两个字段连接到表2中?

我在下面有此代码,我还如何将用户表中的“ mobilenumber”,“ firstname”和“ lastname”字段连接到user_address表中?

 $query = "SELECT * FROM user_address WHERE user_id IN 
  (SELECT id FROM user WHERE email = '".$email."')";

2 个答案:

答案 0 :(得分:0)

$query = "SELECT a.*,b.mobilenumber,b.firstname,b.lastname 
          FROM user_address a 
            left join user b on a.user_id=b.user_id 
          WHERE a.user_id IN 
            (SELECT id FROM user WHERE email = '".$email."')";

答案 1 :(得分:0)

您可以改用LEFT JOIN,然后从两个表中选择属性。示例:

$query = "SELECT ua.*, 
   u.mobilenumber, 
   u.firstname, 
   u.lastname 
FROM   user_address ua 
   LEFT JOIN USER u 
          ON u.id = ua.user_id 
WHERE u.email = '$email'";