I have a table of this type
| user | parents | sons |
with
I write this query:
SELECT user, COALESCE(parents, 0) AS parentsEd, COALESCE(sons, 0) AS sonsEd
FROM my_table
ORDER BY (parentsEd + sonsEd) DESC
and it works! So it give a result with duplicated users. How I can to have DISTINC users by using (parentsEd + sonsEd) DESC ORDER ?
Use GROUP BY
before the order clause and if you want order results first then group them then use subselect
SELECT `user`, COALESCE(parents, 0) AS parentsEd, COALESCE(sons, 0) AS sonsEd
FROM my_table GROUP BY `user` ORDER BY (parentsEd + sonsEd) DESC
SELECT DISTINCT `user`, COALESCE(parents, 0) AS parentsEd, COALESCE(sons, 0) AS sonsEd
FROM my_table ORDER BY (parentsEd + sonsEd) DESC
SELECT a.* FROM (
SELECT DISTINCT `user`, COALESCE(parents, 0) AS parentsEd, COALESCE(sons, 0) AS sonsEd
FROM my_table ORDER BY (parentsEd + sonsEd) DESC ) a GROUP BY a.`user`
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다