我有以下两个表
1. publisher_site_regionwise_adratio
publisher_id | site | region | ad_ratio | product_code
=========================================================
001 | xyz.com | US | 8:2 | TB
和
2. publisher_site_regionwise_info
publisher_id | site | region | regional_keywords
=======================================================
001 | xyz.com | US | business, warehouse
现在第二个表的数据不是product_code明智的。站点的区域信息与具有特定区域的特定站点的发布者的product_code无关。现在我想要一个能给我以下字段的查询
site | region | ad_ratio | has_regional_info
============================================
xyz.com | US | 8:2 | 1
has_regional_info列将具有0或1,具体取决于特定区域中的网站是否已映射regional_keywords。
我无法想象如何使用JOIN并获得这样的结果。任何帮助将非常感激。
答案 0 :(得分:1)
我会在您的信息表格中加入一个简单的JOIN,然后检查regional_keywords
列中是否有数据来确定{1}}的1或0
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
和publisher_site_regionwise_adratio
之间的外键不仅仅是publisher_site_regionwise_info
(在您的示例中难以分辨3个重复的列),那么只需将它们添加到{{1}连接语句如:publisherId
答案 1 :(得分:0)
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
答案 2 :(得分:0)
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
答案 3 :(得分:0)
这应该做:
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;