使用`DISTINCT`关键字时出错?

时间:2014-09-21 16:28:44

标签: mysql sql database select

请查看以下查询

SELECT Client_Portfolio.*,
DISTINCT Client.Name AS "Client Name", 
Portfolio.Portfolio_Type 
FROM Client_Portfolio 
INNER JOIN Client ON Client.idClient = Client_Portfolio.idClient 
INNER JOIN Portfolio ON Portfolio.idPortfolio = Client_Portfolio.idPortfolio 
WHERE Portfolio.Portfolio_Type = "Out"

我想删除重复"客户名称"的记录。所以我使用了关键字distinct。但它给出了以下错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'distinct (Client.Name) AS "Client Name",
Portfolio.Portfolio_Type
FROM Client_' at line 2 

我做错了什么?

1 个答案:

答案 0 :(得分:2)

select distinct的语法就是select distinct

SELECT DISTINCT cp.*, c.Name AS "Client Name", p.Portfolio_Type 
FROM Client_Portfolio cp INNER JOIN
     Client cp
     ON c.idClient = cp.idClient INNER JOIN
     Portfolio
     ON p.idPortfolio = cp.idPortfolio 
WHERE p.Portfolio_Type = 'Out';

它适用于select列表中的所有列。

我不知道这是不是你想要的,但这是正确的语法。

注意我对您的查询进行了其他两项更改。首先,我添加了表别名,以使查询更容易编写和读取。其次,我将字符串常量放在单引号而不是双引号中。