简体   繁体   中英

How do I use more “select query” with Mysql to get result from different table

Well, I've 3 database table.

1) users
2) user_property
3) offers

users table structure:

----------------------------------    
id    pc1   pc2    pc3      pc4
----------------------------------
1     1205  1203   1208    1209
2     1206  1205   1209    1202
3     1207  1204   1210    1201
4     1208  1210   1211    1203
5     1209  1207   1208    1204

user_property table structure:

----------------------------------------------------    
property_id    user_id   postcode    creation_date
----------------------------------------------------
1              1         1205       02-01-2001
2              2         1206       02-03-2001
3              3         1207       02-04-2001
4              4         1205       02-05-2001
5              5         1208       02-06-2001
6              4         1205       02-07-2001

offers table structure:

-------------------------
offer_id      property_id
-------------------------
1              1         
2              2         
3              3         
4              4         
5              5         
6              4         

Query

$myQuery =  mysql_query("
 SELECT * 
   FROM user_property upr 
  WHERE (postcode = '$pc1' OR
         postcode = '$pc2' OR
         postcode = '$pc3' OR
         postcode = '$pc4') AND
         datediff(CURDATE(), upr.creation_date) <= 7 AND
         NOT EXISTS(SELECT ofr.property_id 
                      FROM offers ofr 
                     WHERE ofr.property_id = upr.property_id AND
                           ofr.agent_id IN(SELECT id 
                                             FROM users 
                                            WHERE company_name !=''
                                          )
                   )
ORDER BY property_id DESC");

in this $myQuery query you can see that ( postcode = '$pc1' or postcode = '$pc2' or postcode = '$pc3' or postcode = '$pc4' ) . this postcode is come from users table.

So how can i to get those postcode with 1 sql query from user_property table ?

you also see that I just get id by using IN (select id from users where company_name !='') . If i use same query to get those postcode then it's not working. May be something I mistake.

from our discussion we ended up with:

select 
  users.*, 
  user_property.*, 
  offers.property_id as offers_property_id 
from users 
  join user_property on users.id = user_property.user_id
    and (
          user_property.postcode = users.pc1 
      or  user_property.postcode = users.pc2
      or  user_property.postcode = users.pc3
      or  user_property.postcode = users.pc4
    )
  left join offers on 
    offers.property_id = user_property.property_id
    and offers.agent_id = users.id 

where users.company_name != ""
  and DATEDIFF(CURDATE(), user_property.creation_date) <= 7

HAVING offers_property_id IS NULL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM