我有两张桌子:
users
和users_img
。
我需要创建一个查询,以便从users
表中选择所有用户zip
列不为空并且img
列中的users_images
列为空(两个表都有用户) ID:users
。id
和users_img
。user_id
因此可以加入这些内容。
users
users_ids
id
中的users
相同)我试过了:
SELECT * FROM `users` JOIN `users_ids` on users.id = users_ids.user_id
WHERE `zip` != '' AND `img` = '' ORDER BY `last_time` DESC
没有运气。我知道应该很简单。
答案 0 :(得分:1)
我修改了您的查询以检查Null
OR
值
试试这个:
SELECT * FROM `users`
LEFT JOIN `users_ids` on users.id = users_ids.user_id
WHERE (TRIM(`zip`) != '' OR `zip` is not null) AND
(TRIM(`img`) = '' OR `img` is null) ORDER BY `last_time` DESC
答案 1 :(得分:1)
它也适合你
SELECT * FROM `users` as `u`
JOIN `users_ids` as `uid` on `u`.`id` = `uid`.`user_id`
WHERE `u`.`zip` IS NOT NULL
AND `uid`.`img` IS NULL
ORDER BY `u`.`last_time` DESC
答案 2 :(得分:0)
这有效:
SELECT * FROM `users` as `u`
LEFT JOIN `users_ids` as `uid` on `u`.`id` = `uid`.`user_id`
WHERE `u`.`zip` != ''
AND `uid`.`img` IS NOT NULL
ORDER BY `u`.`last_time` DESC