php - combine two different queries into one -


i have following query:

    select distinct id, title               ((select          distinct offers.id id, offers.title title      offers      inner join categories     on offers.category=categories.title      categories.title="fashion clothes"      group offers.id     order offers.id)     union     (select          distinct offers.id id, offers.title title      offers      inner join cities     on offers.city=cities.title      cities.title="australia"      group offers.id     order offers.id)) subquery 

i fetch table offers rows have category=fashion clothes , city=australia when use union returns rows . don't know how make work. if can appreciate it.

you don't need union this. join tables , have both conditions in clause:

select  distinct offers.id id, offers.title title  offers inner join categories on offers.category=categories.title inner join cities on offers.city=cities.title  categories.title="fashion clothes" , cities.title="australia" order offers.id 

as noted rubahmalam don't need joins joining tables title, query can simplified to:

select  distinct offers.id id, offers.title title  offers  offers.category="fashion clothes" , offers.city="australia" order offers.id 

however, best have separate unique id's in tables , use join them in queries, that's story.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -