Selecting distinct value from a column in MySql

Parveez Ahmed

Suppose,I have a table named items:

sender_id receiver_id goods_id price
  2            1          a1   1000
  3            1          b2   2000
  2            1          c1   5000
  4            1          d1   700
  2            1          b1   500   

Here I want to select the sender_id,goods_id in descending order of price from the items table such that no row appears more than once which contains the same sender_id value (here sender_id 2). I used the following query,but was in vain:

select distinct sender_id,goods_id from items where receiver_id=1 order by price desc

The result shows all the five tuples(records) with the tuples containing sender_id 2 thrice in descending order of time.But what I want is to display only three records one of them having sender_id of 2 with only the highest price of 5000. What should I do? My expected output is:

sender_id goods_id
   2         c1
   3         b2
   4         d1
Tin Tran

please try this

select sender_id,goods_id from items t1
where not exists (select 1 from items t2
                  where t2.sender_id = t1.sender_id
                    and t2.receiver_id = t1.receiver_id
                    and t2.price > t1.price)
 and receiver_id = 1
order by price desc

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Selecting distinct values from a column in Peewee

From Dev

selecting distinct values in mysql from 3 tables

From Dev

Selecting default value of a column from empty table (mysql)

From Dev

How to get count from distinct value one column in MYSQL

From Dev

MySQL Select Query: SUM() rows with a distinct value, from another column

From Dev

MySQL Count values from one column with selecting a value from another column

From Dev

Selecting distinct values from multiple column of a table with their count

From Dev

MySQL query all distinct from column A where none of them has a specific value in column B

From Dev

MySQL query all distinct from column A where none of them has a specific value in column B

From Dev

Update column in Table A from Table B where value does not exist in distinct result from Table B in MYSQL

From Dev

selecting distinct value order by desc

From Dev

MySQL: Selecting distinct with aggregate functions

From Dev

MySQL: Selecting distinct with aggregate functions

From Dev

Select distinct column value from date range

From Dev

select only distinct value from one column

From Dev

Select distinct column value from date range

From Dev

MYSQL: Selecting value in column where column name is defined on a seperate table

From Dev

How to select a column value from the highest distinct value of another column

From Dev

Distinct value from multiple tables in mysql

From Dev

Update a column by selecting random value from a different column

From Dev

Selecting MAX on column then MAX from column that is dependent on first value

From Dev

MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

From Dev

Selecting the value of a column not in GroupBy

From Dev

Mysql Selecting Alias Column

From Dev

Find max column value from distinct column values

From Dev

How read multiple column but with DISTINCT value from one column?

From Dev

Rails query selecting last record with distinct column

From Dev

Rails query selecting last record with distinct column

From Dev

selecting the rows based on the distinct column values

Related Related

  1. 1

    Selecting distinct values from a column in Peewee

  2. 2

    selecting distinct values in mysql from 3 tables

  3. 3

    Selecting default value of a column from empty table (mysql)

  4. 4

    How to get count from distinct value one column in MYSQL

  5. 5

    MySQL Select Query: SUM() rows with a distinct value, from another column

  6. 6

    MySQL Count values from one column with selecting a value from another column

  7. 7

    Selecting distinct values from multiple column of a table with their count

  8. 8

    MySQL query all distinct from column A where none of them has a specific value in column B

  9. 9

    MySQL query all distinct from column A where none of them has a specific value in column B

  10. 10

    Update column in Table A from Table B where value does not exist in distinct result from Table B in MYSQL

  11. 11

    selecting distinct value order by desc

  12. 12

    MySQL: Selecting distinct with aggregate functions

  13. 13

    MySQL: Selecting distinct with aggregate functions

  14. 14

    Select distinct column value from date range

  15. 15

    select only distinct value from one column

  16. 16

    Select distinct column value from date range

  17. 17

    MYSQL: Selecting value in column where column name is defined on a seperate table

  18. 18

    How to select a column value from the highest distinct value of another column

  19. 19

    Distinct value from multiple tables in mysql

  20. 20

    Update a column by selecting random value from a different column

  21. 21

    Selecting MAX on column then MAX from column that is dependent on first value

  22. 22

    MYSQL query - simple way to return all values from one column based on a DISTINCT value in another column in the same table?

  23. 23

    Selecting the value of a column not in GroupBy

  24. 24

    Mysql Selecting Alias Column

  25. 25

    Find max column value from distinct column values

  26. 26

    How read multiple column but with DISTINCT value from one column?

  27. 27

    Rails query selecting last record with distinct column

  28. 28

    Rails query selecting last record with distinct column

  29. 29

    selecting the rows based on the distinct column values

HotTag

Archive