我有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
。 最佳答案
您也可以尝试以下方法:
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小提琴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
fiddle
关于mysql - 从3个不同的表格中选择一列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22269496/