mysql - Select fields from table with DISTINCT field -
i have table of type
| user | parents | sons |
with
- parents , sons columuns can null.
- user column can contains duplicated
i write query:
select user, coalesce(parents, 0) parentsed, coalesce(sons, 0) sonsed my_table order (parentsed + sonsed) desc
and works! give result duplicated users. how can have distinc users using (parentsed + sonsed) desc order ?
use group by
before order clause , if want order results first group them use subselect
select `user`, coalesce(parents, 0) parentsed, coalesce(sons, 0) sonsed my_table group `user` order (parentsed + sonsed) desc select distinct `user`, coalesce(parents, 0) parentsed, coalesce(sons, 0) sonsed my_table order (parentsed + sonsed) desc select a.* ( select distinct `user`, coalesce(parents, 0) parentsed, coalesce(sons, 0) sonsed my_table order (parentsed + sonsed) desc ) group a.`user`
Comments
Post a Comment