MySQL查询条件为

时间:2016-01-17 07:24:46

标签: mysql

我有两张桌子: usersusers_img

我需要创建一个查询,以便从users表中选择所有用户zip列不为空并且img列中的users_images列为空(两个表都有用户) ID:usersidusers_imguser_id因此可以加入这些内容。

users

  1. ID
  2. 名称
  3. 拉链
  4. 时间:LAST_TIMEBREAK
  5. users_ids

    1. ID
    2. user_id(与id中的users相同)
    3. IMG
    4. 我试过了:

      SELECT * FROM `users` JOIN `users_ids` on users.id = users_ids.user_id
       WHERE `zip` != '' AND `img` = '' ORDER BY `last_time` DESC 
      
      没有运气。我知道应该很简单。

3 个答案:

答案 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