
Untitled
By: a guest on
Jun 30th, 2012 | syntax:
None | size: 1.15 KB | hits: 17 | expires: Never
Words unique to a user - where not in
Word Table:
word - varchar(50)
user_id - integer
SELECT
word, count(word) as count
FROM
words
WHERE
word not in (select word from words where user_id <> 99 group by word)
and user_id = 99
GROUP BY word
ORDER BY count desc LIMIT 20
SELECT
word, count(word) as count
FROM
words
WHERE
word not in (select distinct word from words where user_id <> 99)
and user_id = 99
GROUP BY word
ORDER BY count desc LIMIT 20
SELECT
w1.word,
COUNT(w1.word) as count
FROM
words w1
WHERE
NOT EXISTS (
SELECT 1
FROM
words w2
WHERE
w2.user_id <> 99
AND
w1.word = w2.word
)
AND
w1.user_id = 99
GROUP BY
w1.word
ORDER BY
count DESC
LIMIT 20;
SELECT
w1.word,
COUNT(w1.word) AS count
FROM
words w1
LEFT JOIN words w2 ON (w1.word = w2.word AND w1.user_id <> w2.user_id)
WHERE
w1.user_id = 99
AND
w2.word IS NULL
GROUP BY
w1.word
ORDER BY
count DESC
LIMIT 20;
CREATE INDEX idx_word_user ON words ( word, user_id);