在mysql查询中选择唯一的行
我有疑问
SELECT DISTINCT employer.id, employer.name
FROM employers
LEFT JOIN positions ON positions.id = employers.id
LEFT JOIN statuses ON statuses.position = positions.some
WHERE statuses.number = 2
,问题是我需要雇主提供唯一的记录,但我收到重复的记录,因为 statuses.number 确实像 222 6 777 等重复,我只需要获取它们一次。 我不想使用 DISTINCT 还有其他方法吗?
I have query
SELECT DISTINCT employer.id, employer.name
FROM employers
LEFT JOIN positions ON positions.id = employers.id
LEFT JOIN statuses ON statuses.position = positions.some
WHERE statuses.number = 2
And the thing is some i need unique records from employers but i am getting duplicates because statuses.number do repeat like 222 6 777 etc i need to grab them only once.
I dont want to use DISTINCT is there other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 GROUP BY 语句
use the GROUP BY statment
对雇主 ID 使用 GROUP BY,对于返回的每一行,您只会获得一条雇主记录:
Using GROUP BY on employer id, you will only get one employer record for each row returned:
首先,每当您在 WHERE 子句中引用 LEFT JOINed 列(例如
statuses.number =2
)时,您都会否定 LEFT 并强制其表现得像 INNER。其次,尝试这个版本:
First, any time you reference a LEFT JOINed column in your WHERE clause, like
statuses.number =2
, you negate the LEFT and force it to behave like an INNER.Second, try this version: