2014-09-26 34 views
0

我有兩個表格,一個表格和一個表格用戶通知。我必須選擇尚未收到通知的用戶。我嘗試了各種方法,例如使用此查詢:如何在兩個MySQL表之間的對抗中找到缺失的數字?

SELECT user.registration_id FROM user 
LEFT JOIN notify ON user.registration_id = notify.registration_id 

但它不起作用。我該如何解決?

另外你還鏈接了兩個使用的表的結構。提前致謝。

CREATE TABLE IF NOT EXISTS `user` (
    `id` int(15) NOT NULL, 
    `user_id` int(10) unsigned NOT NULL, 
    `registration_id` varchar(200) NOT NULL, 
    `login_username` varchar(100) NOT NULL 
); 

CREATE TABLE IF NOT EXISTS `notify` (
    `id` int(11) unsigned NOT NULL, 
    `registration_id` varchar(200) NOT NULL, 
    `user_id` int(15) unsigned NOT NULL, 
    `start_notify` datetime NOT NULL, 
    `stop_notify` datetime NOT NULL, 
    `partial_score` int(15) unsigned NOT NULL 
); 

回答

0

你需要一個WHERE語句來找到用戶,誰沒有過一個通知尚未:

SELECT user.registration_id 
FROM user 
    LEFT JOIN notify 
    ON user.registration_id = notify.registration_id 
WHERE notify.id IS NULL; 

這將檢索用戶的所有用戶ID沒有在通知中的條目誰表。

+0

真的非常感謝,太容易了!我沒有想到這一點。 – Kappa 2014-09-26 10:38:54

相關問題