MySQL 查询不显示重复的 id
MySQL Query Not show duplicate id
如果我的查询中的重复 id_customer 是,
,我想不显示
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid
FROM
ps_orders
RIGHT JOIN ps_customer
on ps_orders.id_customer = ps_customer.id_customer
WHERE
ps_orders.total_paid > 1
GROUP BY
ps_orders.id_customer;
我正在使用 "group by" 但它没有显示。我希望它不会显示任何内容,即使是
组
这样显示
id_customer firstname lastname email total_paid
--------------------------------------------------------
55 name8 name10 ***@windowslive.com 88.90
58 name6 name7 ****@hotmail.com 78.90
59 name3 name5 ****@hotmail.com 123.83
60 name name2 ****@hotmail.com 78.90
第一次查询是这样的,
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid
FROM
ps_orders
RIGHT JOIN ps_customer
on ps_orders.id_customer = ps_customer.id_customer
WHERE
ps_orders.total_paid > 1
ORDER BY
ps_orders.id_customer;
没有分组依据
id_customer firstname lastname email total_paid
--------------------------------------------------------
55 name8 name10 ***@windowslive.com 88.90
58 name6 name7 ****@hotmail.com 56.00
58 name6 name7 ****@hotmail.com 87.90
58 name6 name7 ****@hotmail.com 79.99
58 name6 name7 ****@hotmail.com 78.90
59 name3 name5 ****@hotmail.com 123.83
60 name name2 ****@hotmail.com 78.90
但我想这样看
id_customer firstname lastname email total_paid
--------------------------------------------------------
55 name8 name10 ***@windowslive.com 88.90
59 name3 name5 ****@hotmail.com 123.83
60 name name2 ****@hotmail.com 78.90
您需要检查 HAVING
:
http://sqlfiddle.com/#!9/a981f/1
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid
FROM
ps_orders
RIGHT JOIN ps_customer
on ps_orders.id_customer = ps_customer.id_customer
GROUP BY
ps_orders.id_customer
HAVING COUNT(*)=1
试试这个:
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid
FROM
ps_orders
JOIN ps_customer
on ps_orders.id_customer = ps_customer.id_customer
WHERE
ps_orders.total_paid > 1
AND ps_orders.id_customer IN ( SELECT ps_orders.id_customer
FROM ps_orders
GROUP BY ps_orders.id_customer
HAVING COUNT(1) < 2 )
如果我的查询中的重复 id_customer 是,
,我想不显示SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid
FROM
ps_orders
RIGHT JOIN ps_customer
on ps_orders.id_customer = ps_customer.id_customer
WHERE
ps_orders.total_paid > 1
GROUP BY
ps_orders.id_customer;
我正在使用 "group by" 但它没有显示。我希望它不会显示任何内容,即使是
组这样显示
id_customer firstname lastname email total_paid
--------------------------------------------------------
55 name8 name10 ***@windowslive.com 88.90
58 name6 name7 ****@hotmail.com 78.90
59 name3 name5 ****@hotmail.com 123.83
60 name name2 ****@hotmail.com 78.90
第一次查询是这样的,
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid
FROM
ps_orders
RIGHT JOIN ps_customer
on ps_orders.id_customer = ps_customer.id_customer
WHERE
ps_orders.total_paid > 1
ORDER BY
ps_orders.id_customer;
没有分组依据
id_customer firstname lastname email total_paid
--------------------------------------------------------
55 name8 name10 ***@windowslive.com 88.90
58 name6 name7 ****@hotmail.com 56.00
58 name6 name7 ****@hotmail.com 87.90
58 name6 name7 ****@hotmail.com 79.99
58 name6 name7 ****@hotmail.com 78.90
59 name3 name5 ****@hotmail.com 123.83
60 name name2 ****@hotmail.com 78.90
但我想这样看
id_customer firstname lastname email total_paid
--------------------------------------------------------
55 name8 name10 ***@windowslive.com 88.90
59 name3 name5 ****@hotmail.com 123.83
60 name name2 ****@hotmail.com 78.90
您需要检查 HAVING
:
http://sqlfiddle.com/#!9/a981f/1
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid
FROM
ps_orders
RIGHT JOIN ps_customer
on ps_orders.id_customer = ps_customer.id_customer
GROUP BY
ps_orders.id_customer
HAVING COUNT(*)=1
试试这个:
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid
FROM
ps_orders
JOIN ps_customer
on ps_orders.id_customer = ps_customer.id_customer
WHERE
ps_orders.total_paid > 1
AND ps_orders.id_customer IN ( SELECT ps_orders.id_customer
FROM ps_orders
GROUP BY ps_orders.id_customer
HAVING COUNT(1) < 2 )