我有3张桌子:
Table 1: columns are:
+------+-------+----------+----------+
| date | time | user_id | channel |
+------+-------+----------+----------+
Table 2:
+---------+------------+
| user_id | id _number |
+---------+------------+
Table 3:
+---------+-------+
| channel | sort |
+---------+-------+
In table 1 the columns are sorted as following :
2011-07-29, 12:35:15.650, 22, DeluxeMusic
In table 2 :
130.83.10.c42ce82365b9f6d , 22 (same as user_id in table 1)
In table 3:
DeluxeMusic (same as in table 1), entertainment.
user_id
中的table 2
列:130.83.10.c42ce82365b9f6d
表示某个用户的user_id。从这个用户ID,我需要在开始时获得值为130
的所有条目。对于具有相同值130的所有条目。
然后我需要在130
他们正在观看的频道中寻找table 3
的这个值,并根据观看频道类型的所有用户来计算。
频道类型也是:sport, shopping, entertainment and so on
。
答案 0 :(得分:0)
不确定我是否完全接受了您的问题。但据我所知,你需要的东西如下:
SELECT COUNT (*) FROM TABLE1 T1,TABLE2 T2,TABLE3 T3
WHERE T1.USER_ID = T2.USER_ID
AND T1.USER_ID LIKE '130%'
AND T1.CHANNEL = T3.CHANNEL
上面的sql将用于获取以“130”开头的用户ID正在观看的用户的频道数。
希望这会对你有所帮助。
答案 1 :(得分:0)
你也可以试试这个:
select count(*) from t1 join t2 on t1.id = t2.id and t2.userid like '130%' join
t3 on t1.channel = t3.channel group by t1.channel
sql fiddle here
修改强>
另一个版本:
select count(t1.id) , t1.*, t2.*, t3.*
from t1 join t2 on t1.id = t2.id join
t3 on t1.channel = t3.channel and t2.userid
like '130%' group by t3.channel, t1.id