Mysql select distinct

前端 未结 5 1886
孤街浪徒
孤街浪徒 2020-12-01 06:25

I am trying to select of the duplicate rows in mysql table it\'s working fine for me but the problem is that it is not letting me select all the fields in that query , just

相关标签:
5条回答
  • 2020-12-01 06:41

    DISTINCT is not a function that applies only to some columns. It's a query modifier that applies to all columns in the select-list.

    That is, DISTINCT reduces rows only if all columns are identical to the columns of another row.

    DISTINCT must follow immediately after SELECT (along with other query modifiers, like SQL_CALC_FOUND_ROWS). Then following the query modifiers, you can list columns.

    • RIGHT: SELECT DISTINCT foo, ticket_id FROM table...

      Output a row for each distinct pairing of values across ticket_id and foo.

    • WRONG: SELECT foo, DISTINCT ticket_id FROM table...

      If there are three distinct values of ticket_id, would this return only three rows? What if there are six distinct values of foo? Which three values of the six possible values of foo should be output?
      It's ambiguous as written.

    0 讨论(0)
  • 2020-12-01 06:46

    You can use group by instead of distinct. Because when you use distinct, you'll get struggle to select all values from table. Unlike when you use group by, you can get distinct values and also all fields in table.

    0 讨论(0)
  • 2020-12-01 06:46

    use a subselect:

    http://forums.asp.net/t/1470093.aspx

    0 讨论(0)
  • 2020-12-01 06:47

    You can use DISTINCT like that

    mysql_query("SELECT DISTINCT(ticket_id), column1, column2, column3 
    FROM temp_tickets 
    ORDER BY ticket_id");
    
    0 讨论(0)
  • 2020-12-01 06:59

    Are you looking for "SELECT * FROM temp_tickets GROUP BY ticket_id ORDER BY ticket_id ?

    UPDATE

    SELECT t.* 
    FROM 
    (SELECT ticket_id, MAX(id) as id FROM temp_tickets GROUP BY ticket_id) a  
    INNER JOIN temp_tickets t ON (t.id = a.id)
    
    0 讨论(0)
提交回复
热议问题