显示我没有投票的用户 - MySQL

时间:2012-03-20 03:04:02

标签: php mysql sql

我正在尝试显示我尚未投票的所有用户!

CREATE TABLE IF NOT EXISTS `users` 
(`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(200) COLLATE utf8_unicode_ci NOT NULL,PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `whose_voted` (
`voter_user_id` int(11) NOT NULL,
`voted_on_user_id` int(11) NOT NULL,
PRIMARY KEY (`voter_user_id`,`voted_on_user_id`)
)

CREATE TABLE IF NOT EXISTS `votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`yes_count` int(11) NOT NULL,
`beer_count` int(11) NOT NULL,
`total_votes` int(11) NOT NULL,
 PRIMARY KEY (`id`)
)

CREATE TABLE IF NOT EXISTS `user_votes` (
`users_id` int(11) NOT NULL,
`votes_id` int(11) NOT NULL,
 PRIMARY KEY (`users_id`)
)

我的查询工作正常:

/* Show the Names of all users that have not been Voted on */
SELECT users.name
FROM users
 LEFT JOIN user_votes ON users.id = users_id
 LEFT JOIN votes ON votes_id = votes.id
WHERE votes.id IS NULL
ORDER BY users.name

此查询也有效

/* Show all the User Names that I have voted on */
SELECT users.id AS 'User ID', users.name AS 'I\'ve Voted On'
FROM users
 INNER JOIN whose_voted ON users.id = voted_on_user_id
WHERE whose_voted.voter_user_id = 32 /* my User ID */

我坚持向用户展示我还没有投票!

** 更新了工作查询 ****

/* Show a random users I have Not Voted For as of yet */
SELECT
 unot.id, unot.name
FROM
 users AS unot
WHERE NOT EXISTS (
 /* Exclude the users you have voted on from the total user list */
 SELECT uhave.id
 FROM users AS uhave
  INNER JOIN whose_voted ON uhave.id = voted_on_user_id
 WHERE unot.id = uhave.id 
  AND (whose_voted.voter_user_id = 32)
) 
AND (unot.id != 32)
AND (RAND()<(SELECT ((1/COUNT(*))*10) FROM users))
ORDER BY RAND() 
LIMIT 1;

这个坏男孩在我没有投票的时间吐出一个随机用户!感谢您的帮助,非常有用!

你可以想象,我正在对随机的人进行投票,这会将他们放入who_voted表中,因此没有再显示随机查询......任何想要简化我的代码的SQL大师?干杯米克

3 个答案:

答案 0 :(得分:0)

一种方法是使用上面最后一个查询的WHERE NOT EXISTS作为子查询。当然没有经过测试,但应该有效。

SELECT
  users.id AS `User ID`, users.name AS 'I\'ve not Voted On'
FROM
  users unot
WHERE NOT EXISTS (
    /* Exclude the users you have voted on from the total user list */
    SELECT users.id AS
    FROM users uhave
     INNER JOIN whose_voted ON users.id = voted_on_user_id
    WHERE whose_voted.voter_user_id = 32 /* my User ID */
      AND unot.id = uhave.id
)

虽然NOT EXISTS子查询可能比JOIN慢。再次,未经测试。

SELECT
  users.id AS `User ID`, 
  users.name AS 'I\'ve not Voted On'
FROM
  users unot
  LEFT JOIN (
    SELECT users.id AS
    FROM users uhave
     INNER JOIN whose_voted ON users.id = voted_on_user_id
    WHERE whose_voted.voter_user_id = 32 /* my User ID */
  ) uhave ON unot.id = uhave.id
/* NULL in the have voted subquery indicates user hasn't been voted on. */
WHERE uhave.id IS NULL

答案 1 :(得分:0)

也许我只是过度简化它,这不会起作用吗?

SELECT users.name 
FROM users
LEFT JOIN whose_voted ON users.id = voted_on_user_id
WHERE whose_voted.voter_user_id != 32

答案 2 :(得分:0)

尝试此查询

$yourId=7;

SELECT id,users.name 
FROM users
where id not in 
(select distinct(voted_on_user_id) 
 from whose_voted
 where voter_user_id =$yourId)