Select fields from table with DISTINCT field

Tab

I have a table of this type

| user | parents | sons |

with

  • parents and sons columuns which can be NULL.
  • user column can contains duplicated

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 ?

M Khalid Junaid

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Select multiple fields from columns with distinct and limit

분류에서Dev

Select all fields from a table where a field in another table with an ID is equal to a string

분류에서Dev

How to SELECT distinct with MAX and return full table fields

분류에서Dev

select distinct from elasticsearch

분류에서Dev

Replace field with field from other table in SELECT query

분류에서Dev

Postgresql : select distinct * from func ();

분류에서Dev

Select rows from DB in dependence of field value in child table

분류에서Dev

Dynamic table name in where clause using a field from select

분류에서Dev

쿼리 성능 비교 : Join Vs Select Distinct From Table

분류에서Dev

Django 모델-SELECT DISTINCT (foo) FROM table이 너무 느립니다.

분류에서Dev

select * from (select * from table)

분류에서Dev

How to cross join a table and use fields from the select statment in the where clause of the cross join

분류에서Dev

SQL Server : SELECT DISTINCT [COL1] from {TABLE} WHERE [COL2] = 'A'AND [COL2] <> 'B'

분류에서Dev

SQL Server : SELECT DISTINCT [COL1] from {TABLE} WHERE [COL2] = 'A'AND [COL2] <> 'B'

분류에서Dev

MongoDB 집계 쿼리 대 MySQL SELECT field1 FROM table

분류에서Dev

SELECT FROM table, ORDER BY IF (...)

분류에서Dev

Sytax error when select distinct rows in a sql table

분류에서Dev

select a field values that are not found in another table

분류에서Dev

SQL query to dynamically COUNT(FIELD) for all fields of table X

분류에서Dev

How to select all non-distinct rows from a DB?

분류에서Dev

MySQL Select Distinct values from 2 tables and Count

분류에서Dev

Select from another select without temporary table?

분류에서Dev

SELECT array of cols FROM table

분류에서Dev

RegEx in select from table in db

분류에서Dev

SELECT * FROM table Where CURDATE ()

분류에서Dev

CREATE TABLE AS select * from partitioned table

분류에서Dev

SELECT * INTO [newdatabase]. [table] FROM [otherdatabase]. [table]

분류에서Dev

PostgreSQL : SELECT DISTINCT 대 SELECT DISTINCT ON (id)

분류에서Dev

Pull field from another table in MVC

Related 관련 기사

  1. 1

    Select multiple fields from columns with distinct and limit

  2. 2

    Select all fields from a table where a field in another table with an ID is equal to a string

  3. 3

    How to SELECT distinct with MAX and return full table fields

  4. 4

    select distinct from elasticsearch

  5. 5

    Replace field with field from other table in SELECT query

  6. 6

    Postgresql : select distinct * from func ();

  7. 7

    Select rows from DB in dependence of field value in child table

  8. 8

    Dynamic table name in where clause using a field from select

  9. 9

    쿼리 성능 비교 : Join Vs Select Distinct From Table

  10. 10

    Django 모델-SELECT DISTINCT (foo) FROM table이 너무 느립니다.

  11. 11

    select * from (select * from table)

  12. 12

    How to cross join a table and use fields from the select statment in the where clause of the cross join

  13. 13

    SQL Server : SELECT DISTINCT [COL1] from {TABLE} WHERE [COL2] = 'A'AND [COL2] <> 'B'

  14. 14

    SQL Server : SELECT DISTINCT [COL1] from {TABLE} WHERE [COL2] = 'A'AND [COL2] <> 'B'

  15. 15

    MongoDB 집계 쿼리 대 MySQL SELECT field1 FROM table

  16. 16

    SELECT FROM table, ORDER BY IF (...)

  17. 17

    Sytax error when select distinct rows in a sql table

  18. 18

    select a field values that are not found in another table

  19. 19

    SQL query to dynamically COUNT(FIELD) for all fields of table X

  20. 20

    How to select all non-distinct rows from a DB?

  21. 21

    MySQL Select Distinct values from 2 tables and Count

  22. 22

    Select from another select without temporary table?

  23. 23

    SELECT array of cols FROM table

  24. 24

    RegEx in select from table in db

  25. 25

    SELECT * FROM table Where CURDATE ()

  26. 26

    CREATE TABLE AS select * from partitioned table

  27. 27

    SELECT * INTO [newdatabase]. [table] FROM [otherdatabase]. [table]

  28. 28

    PostgreSQL : SELECT DISTINCT 대 SELECT DISTINCT ON (id)

  29. 29

    Pull field from another table in MVC

뜨겁다태그

보관