我正在尝试列出下订单的客户姓名,他们来自某个县并从某个销售代表下订单。问题是,决赛桌会多次打印出他们的名字。我如何让他们每个人只打印一次名字?
SELECT cName
FROM customer INNER JOIN orders ON customer.cNo = orders.cNO,author
WHERE county = "Galway" AND saleRepName = "Jane Smith";
我正在尝试列出下订单的客户姓名,他们来自某个县并从某个销售代表下订单。问题是,决赛桌会多次打印出他们的名字。我如何让他们每个人只打印一次名字?
SELECT cName
FROM customer INNER JOIN orders ON customer.cNo = orders.cNO,author
WHERE county = "Galway" AND saleRepName = "Jane Smith";
您可以使用Distinct
,但如果有很多记录,您可能会受到性能影响
SELECT DISTINCT
cName
FROM customer
INNER JOIN orders ON customer.cNo = orders.cNO,author
WHERE county = "Galway" AND saleRepName = "Jane Smith";
Group By
可能会更快,并允许您获得计数的额外好处:
SELECT
count(1),
cName
FROM customer
INNER JOIN orders ON customer.cNo = orders.cNO,author
WHERE county = "Galway" AND saleRepName = "Jane Smith";
GROUP BY cName
这将打印出如下内容:
3 John Doe
6 Jane Smith
1 Jersey Mike