0
votes

Well, In mysql database there 2 table.

a) users table structure

------------------------
id            user_email
------------------------
1             [email protected]
2             [email protected]
3             [email protected]
6             [email protected]
7             [email protected]
9             [email protected]

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

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. But how do I get it with a single mysql query ?

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

1
By using JOIN ?Mr. Alien
as @Mr.Alien say, if you don't know how to , this is the tutorial http://www.w3schools.com/sql/sql_join.aspKrofz
You may also use the UNION operator.ascx

1 Answers

3
votes

I am giving the answer of your question on some assumption basis, try to implement it with your query, Use 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 .

  • Your users table has some fields and users_property have postcode, address, etc..

  • You can add the fields name if you want to retrieve it from 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");