sql-server – 如何在SQl Server 2008中选择distinct,但仅限于多
发布时间:2021-01-21 13:22:42 所属栏目:MsSql教程 来源:网络整理
导读:我有一个问题: SELECT Content.content_name,Language2.Name,Language2.language_id,Content.id,Content.content_description,FROM Language AS Language2 LEFT JOIN contents AS Content ON (Language2.language_id = Content.language_id) 如何仅选择不同
我有一个问题: SELECT Content.content_name,Language2.Name,Language2.language_id,Content.id,Content.content_description,FROM Language AS Language2 LEFT JOIN contents AS Content ON (Language2.language_id = Content.language_id) 如何仅选择不同的content_name? 解决方法你做这个:SELECT DISTINCT Content.content_name FROM Language AS Language2 LEFT JOIN contents AS Content ON (Language2.language_id = Content.language_id) 那么为什么这不能回答你的问题呢? 让我们考虑以下数据(仅前两列): content_name Name XXXXX 1234 XXXXX 5678 SELECT DISTINCT意味着你只想要一行,但你想要什么名字? 您需要做的是重写代码以使用GROUP BY并为其他列选择适当的聚合函数: SELECT Content.content_name,MIN(Language2.Name) AS Name,MIN(Language2.language_id) AS language_id,MIN(Content.id) AS id,MIN(Content.content_description) AS content_description,FROM Language AS Language2 LEFT JOIN contents AS Content ON (Language2.language_id = Content.language_id) GROUP BY Content.content_name 现在,这可能不会产生你想要的东西,但有一件事是肯定的,你不能欺骗数据库引擎只是“选择其中一行返回,我不关心哪一行.” (编辑:西双版纳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |