MySQL查询交集
MySQL表
CREATE TABLE `viewhistory` ( `viewid` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) NOT NULL, `video` varchar(255) NOT NULL, `viewtime` datetime NOT NULL, PRIMARY KEY (`viewid`) )
如何查询uid=1和uid=2两个用户都观看过的视频(video)?
方法一:嵌套子查询 SELECT video from viewhistory where uid=2 and video in (SELECT video from viewhistory where uid=1)
方法二:自连接查询 SELECT a.video from viewhistory a INNER JOIN viewhistory b on a.video=b.video where a.uid=1 and b.uid=2