[英]How do I get 2 table data in one mysql query
Well, In mysql database there 2 table. 好吧,在mysql数据库中有2个表。
a) users table structure 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 b)用户属性
-------------------------------------------------------------------------------------------
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
So in users table there are user email address exists. 因此,在用户表中存在用户电子邮件地址 。 So I want to get few user_property table data with email address from users table. 所以我想从用户表中获取一些带有电子邮件地址的user_property表数据。 But how do I get it with a single mysql query ? 但是,如何通过单个mysql查询获得它?
$sql = mysql_query("select user_id, postcode, paddress, psuburb, pvalue, creation_date FROM
user_property ORDER BY property_id DESC");
In my users table email address field name is user_email and id field name is id . 在我的用户表中,电子邮件地址字段名称为user_email ,id字段名称为id 。
I am giving the answer of your question on some assumption basis, try to implement it with your query, Use JOIN : 我在某种假设的基础上给出了您问题的答案,请尝试使用查询来实现它,请使用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");
Assumptions are: 假设是:
There are 2 tables users
and users_property
. 有2个表users
和users_property
。
Your users table has some fields and users_property have postcode, address, etc.. 您的用户表具有一些字段,users_property具有邮政编码,地址等。
You can add the fields name if you want to retrieve it from users_property
. 如果要从users_property
检索字段名称,则可以添加字段名称。
Updated Answer: 更新的答案:
$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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.