我需要一个SQL查询,向我显示所有民意调查用户(uid)尚未投票。
示例:
所以我需要向我展示他还没投票的pid 2的查询。
这是两个表:
CREATE TABLE IF NOT EXISTS `poll` (
`pid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) NOT NULL,
`description` text NOT NULL,
`deadline` datetime NOT NULL,
PRIMARY KEY (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `votes` (
`vid` int(11) NOT NULL AUTO_INCREMENT,
`pid` int(11) NOT NULL,
`uid` varchar(20) NOT NULL,
`tid` int(11) NOT NULL,
`votes` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`vid`),
KEY `pcid` (`pid`,`uid`),
KEY `uid` (`uid`),
KEY `tid` (`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;
//编辑这个应该让它更清晰
表votes
ALTER TABLE `votes`
ADD CONSTRAINT `votes_ibfk_3` FOREIGN KEY (`pid`) REFERENCES `poll` (`pid`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `votes_ibfk_4` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `votes_ibfk_5` FOREIGN KEY (`tid`) REFERENCES `teams` (`tid`) ON DELETE CASCADE ON UPDATE CASCADE;
请smbdy请帮助我。我想这是一个WHERE而不是喜欢的连接,但我只是不明白。
的Merci!
答案 0 :(得分:3)
SELECT * FROM Poll WHERE pid NOT IN (SELECT pid FROM votes WHERE uid = 1);
我不是100%确定这会有效,因为我不知道您尝试检索数据的数据库类型。
答案 1 :(得分:1)
SELECT
[poll].*
FROM
[poll]
LEFT OUTER JOIN [votes]
ON [votes].[pid] = [poll].[pid]
AND [votes].[uid] = @uid
WHERE
[votes].[vid] IS NULL
答案 2 :(得分:0)
Select
p.*
From
poll p
Where
Not Exists (
Select
'x'
From
Votes v
Where
v.pid = p.pid and
v.uid = <myuidvalue>
)