0

I have this structure tbl_site_urls: storing urls of a site

tbl_camp_urls: storing urls of a campaign, after handling these urls tbl_site_urls

id - site_id - url
1  - 181     - url1
2  - 182     - url2
3  - 189     - url3

tbl_camp_urls

id - camp_id - site_id - url
1  - 5       - 181     - url1

if i want to list the urls need to be handle

select url from tbl_site_urls
  join site on tbl_site_urls.site_id = site.id AND site.valid=1

then in a loop operation I insert the handled urls into the tbl_camp_urls

I want to list the urls that which have not handled yet. it is like a comparison operation.

The results I'm expecting is

2  - 182     - url2
3  - 189     - url3

3 Answers 3

4
SELECT a.urls
FROM tbl_site_urls as a
LEFT JOIN tbl_camp_urls as b
ON a.site_id = b.site_id
WHERE b.site_id IS NULL;
2
  • This is the correct answer, as it does not require returning all the rows of the second table.
    – dotancohen
    Dec 8, 2013 at 18:50
  • Perfect...you saved my butt. My head is so blocked right now that I couldn't think of the last part WHERE b.site_id IS NULL. Thanks. +1.
    – Fr0zenFyr
    May 12, 2014 at 7:27
1

This should do it:

select  * from tbl_site_urls where url not in (select url from tbl_camp_urls)

(search for subqueries for more info on it)

0
0

My logic tells me that you need to make a intersection to know the commons values and a union to remove them:

should be something like this:

First get all the values:

(select site_id from tbl_site_urls) 
union 
(select id from site)

This is how you get the commons values ONLY

(select url from tbl_site_urls inner join site on tbl_site_urls.site_id = site.id)

Now lets put those queries together:

select * from (select site_id from tbl_site_urls 
union 
select id from site) as tem_table
where
site_id not in
(select url from tbl_site_urls inner join site on tbl_site_urls.site_id = site.id) --commons values
2
  • that only will give you the values from 1 table that are not repeated in the 2 table, but not in both ways...try to put a different value in tbl_camp_urls table and it wont show up
    – jcho360
    Dec 7, 2012 at 13:55
  • As far as I can see that is what the OP wants... ? Dec 7, 2012 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.