我需要将3个表连接到一个表。这是场景。如何将3个表连接到SQL中的单个表中
表1:已user_id说明,APP_ID
表2:已user_id说明,性别
表3:已APP_ID,APP_NAME(PC,移动电话)
表4:已user_id说明,user_city
现在我正在寻找通过移动男性,移动女性,PC男性,PC女性按城市分组的用户数。
这是我的SQL。但是,我想我对联接做错了。请纠正我。
SELECT
t4.user_city,
sum(case when t2.gender = 'm' and t3.app_name = 'PC' then 1 else 0 end) pcmale,
sum(case when t2.gender = 'f' and t3.app_name = 'PC' then 1 else 0 end) pcfemale,
sum(case when t2.gender = 'm' and t3.app_name = 'Mobile' then 1 else 0 end) mobilemale,
sum(case when t2.gender = 'm' and t3.app_name = 'Mobile' then 1 else 0 end) mobilefemale,
FROM
table1 t1
inner join
table2 t2 on t1.user_id = t2.user_id
inner join
table3 t3 on t1.app_id = t3.app_id
inner join
table4 t4 on t1.user_id = t4.user_id
GROUP BY 1
我们可以按照我在查询中完成的方式将多个表连接到单个表中。请帮忙
它运行。我只是不确定我是否正确地做出了正确的结果。您是否发现将多个表连接到t1表的任何问题? – 2014-11-25 01:18:22
移动女性应该检查f,你正在检查m和组应该在t4.user_city – radar 2014-11-25 01:25:17
那么你会得到什么结果? – DeanOC 2014-11-25 01:43:00