MySQL:选择问题

标签 mysql sql select join distinct

有两个表:

wes - contains 8000 rows, with 2000 distinct masternames (mastername is a column in wes)
wem - contains 2100 rows, with 2100 distinct names (name is a column in wem)

我正在尝试获取 wem 中存在的名称,但不是 wes 中的主名称:

SELECT name FROM wem WHERE name NOT IN (SELECT DISTINCT mastername FROM wes)

但出于某种原因,我的查询没有返回任何行,即使 wem 中有大约 100 个名称在 wes 中不作为主名称存在。

有什么问题吗?

谢谢!

最佳答案

您确定子选择中没有 NULLS 吗?

尝试 SELECT mastername FROM wes WHERE mastername IS NULL

如果有,您的查询将返回 0 个 rwow。

调整为:

SELECT NAME FROM wem
WHERE NAME NOT IN (SELECT mastername FROM wes WHERE mastername IS NOT NULL)

关于MySQL:选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27313644/

相关文章:

Android SQLite Select查询速度

mysql - 将一个表中的非重复行插入到另一个 mysql

php - 对mysql中的连接表应用限制

sql - 有没有一种方法可以选择SQL中的成对列表中的行?

sql - 时间戳列是否唯一?

mysql - 从 mysql 选择具有引用 id 的行到同一表

mysql - 将值拆分为其他几列

php - Ckeditor 并使用 $_POST 插入 Mysql

java - UPDATE 后 SELECT 查询的意外行为

.net - .Net SQLConnection 类是否有更安全的替代方案?