我有一个问题:
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
现在,这可能不会产生你想要的东西,但有一件事是肯定的,你不能欺骗数据库引擎只是“选择其中一行返回,我不关心哪一行.”