Reputation: 8360
I have following two tables
1. publisher_site_regionwise_adratio
publisher_id | site | region | ad_ratio | product_code
=========================================================
001 | xyz.com | US | 8:2 | TB
and
2. publisher_site_regionwise_info
publisher_id | site | region | regional_keywords
=======================================================
001 | xyz.com | US | business, warehouse
Now second table has data that is not product_code wise. Regional info for sites is irrespective of product_code for a publisher with a particular site for a particular region. Now I want a query that will give me following fields
site | region | ad_ratio | has_regional_info
============================================
xyz.com | US | 8:2 | 1
has_regional_info column will have 0 or 1 depending upon whether a site in a particular region has regional_keywords mapped or not.
I can't possibly imagine how I can use JOIN and get such result. Any help will be very appreciated.
Upvotes: 2
Views: 121
Reputation: 19466
This should do:
SELECT a.publisher_id, a.site, a.ad_ratio,
i.regional_keywords IS NOT NULL AS has_regional_keywords
FROM publisher_site_regionwise_adratio a, publisher_site_regionwise_info i
WHERE a.publisher_id = i.publisher_id;
Upvotes: 0
Reputation: 3905
select publisher_site_regionwise_adratio.site,
publisher_site_regionwise_adratio.region,
publisher_site_regionwise_adratio.ad_ratio,
LENGTH(regional_keywords)>0 as has_regional_info
from publisher_site_regionwise_adratio
join publisher_site_regionwise_info.publisher_id
on publisher_site_regionwise_adratio.publisher_id
= publisher_site_regionwise_info.publisher_id
Upvotes: 0
Reputation: 17540
I would take a simple JOIN into your info table, then check if there is data in the regional_keywords
column to determine your 1 or 0 for has_regional_info
SELECT ar.site, ar.region, ar.ad_ratio
, CASE
WHEN i.regional_keywords IS NOT NULL THEN 1
ELSE 0
END AS has_regional_info
FROM publisher_site_regionwise_adratio AS ar
LEFT JOIN publisher_site_regionwise_info AS i ON ar.publisherId = i.publisherId
If your foreign key between publisher_site_regionwise_adratio
and publisher_site_regionwise_info
is more than just publisherId
(difficult to tell with 3 repeated columns in your example) then just add these to the ON
statement of the join like: AND ar.otherColumn = i.otherColumn
Upvotes: 1
Reputation: 360702
SELECT ratio.site, ratio.region, ratio.ad_ration,
IF(info.has_regional_info IS NULL, true, false)
FROM publisher_site_regionwise_adratio AS ratio
LEFT JOIN publisher_site_regionwise_info AS info
ON ratio.publisher_id = info.publisher_id
Upvotes: 0