a)用户表格结构
------------------------
id user_email
------------------------
1 email1@yahoo.com
2 email2@yahoo.com
3 email3@yahoo.com
6 email6@yahoo.com
7 email7@yahoo.com
9 email9@yahoo.com
b)user_property
-------------------------------------------------------------------------------------------
property_id user_id pcode pvalue paddress psuburb creation_date
-------------------------------------------------------------------------------------------
10 1 11205 $100 address suburb 10-02-2014
11 2 11205 $100 address suburb 10-02-2014
12 3 11205 $100 address suburb 10-02-2014
13 10 11205 $100 address suburb 10-02-2014
14 12 11205 $100 address suburb 10-02-2014
因此,在用户表中存在用户电子邮件地址。因此,我想从用户表中获取包含电子邮件地址的 user_property 表格数据。但是如何使用单个mysql查询获取它?
$sql = mysql_query("select user_id, postcode, paddress, psuburb, pvalue, creation_date FROM
user_property ORDER BY property_id DESC");
在我的用户表中,电子邮件地址字段名称为 user_email ,ID字段名称为 id 。
答案 0 :(得分:3)
我在某些假设的基础上给出了您的问题的答案,尝试使用您的查询实现它,使用JOIN:
$sql = mysql_query("select u.*, up.postcode, up.paddress FROM users as u JOIN users_property as up on u.email=up.user_email ORDER BY up.property_id DESC");
假设是:
有2个表users
和users_property
。
您的users表有一些字段,users_property有邮政编码, 地址等。
如果要从中检索字段名称,可以添加字段名称
users_property
。
更新的答案:
$sql = mysql_query("select u.id,up.* FROM users as u JOIN users_property as up on u.email=up.user_email ORDER BY up.property_id DESC");