mysql中如何按条件去重

2024年12月03日 01:53
有3个网友回答
网友(1):

利用group by
代码如下:
SELECT * FROM(
select * from customer where user=(
SELECT source_user from customer WHERE user='admin') UNION ALL select * from customer where user=(
select source_user from customer where user=(
SELECT source_user from customer WHERE user='admin')) union ALL select * from customer where user=(
select source_user from customer where user=(
select source_user from customer where user=(
SELECT source_user from customer WHERE user='admin'))) UNION ALL select * from customer where source_user=(/*我的上线的上线的user*/
select user from customer where user=(
select source_user from customer where user=(
SELECT source_user from customer WHERE user='admin'))) union all select * from customer where source_user=(/*我的上线的上线的上线user*/
select user from customer where user=(
select source_user from customer where user=(
select source_user from customer where user=(
SELECT source_user from customer WHERE user='admin'))))) as alias group by user;

网友(2):

直接查出来,通过数组循环,非空的以GRADE为key值,进行重铸数组就可以了。不需要去复杂的查库。

网友(3):

考虑使用union连接两种情况的数据,给个示例:
select id, name, grade from `text` where grade is not null group by grade
union all
select id, name, grade from `text` where grade is null