mysql - Select unique column based on max value of another column in a different table

C_B

I'm trying to select only the highest kw (model table) for each category (type table).

Model table

+-----+----+---------+
| id  | kw | type_id |
+-----+----+---------+
|   1 |  2 |       1 |
|   2 |  5 |       1 |
|   3 | 10 |       2 |
|   4 |  4 |       2 |
|   5 |  5 |       2 |
|   6 |  4 |       3 |
|   7 |  3 |       4 |
|   8 |  7 |       5 |
+-----+----+---------+

Type table

+-----+----------+
| id  | category |
+-----+----------+
|   1 |        1 | 
|   2 |        1 |
|   3 |        2 |
|   4 |        2 |
|   5 |        2 |
+-----+----------+

Attempts
1. this query returns a list of all the kws and categories:

SELECT A.kw, B.category
FROM AC_MODEL A
INNER JOIN AC_TYPE B ON A.type_id = B.id
ORDER BY A.kw DESC 

2. I tried to do something like this answer but it doesn't work:

SELECT A.kw, B.category
FROM AC_MODEL A
INNER JOIN AC_TYPE B ON A.type_id = B.id
ORDER BY A.kw DESC 
WHERE (A.kw, B.category) IN (
    SELECT MAX(A.kw), B.category 
    FROM AC_MODEL A
    INNER JOIN AC_TYPE B ON A.type_id = B.id
    GROUP BY B.category
)

Does anybody have an idea?

Matt

Use MAX and GROUP BY

SELECT MAX(m.kw), t.category
FROM model m
INNER JOIN type t ON m.type_id = t.id
GROUP BY t.category

OUTPUT

MAX(m.kw)   category
10          1
7           2

SQL FIDDLE: http://sqlfiddle.com/#!9/5d0df/5/0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL: Search based on a column, each unique value in another column and max time

From Dev

Get value based on max of a different column grouped by another column

From Dev

SELECT Mysql - Replacing value in one column based on another column

From Dev

select mysql column based on occurrence and value in another column

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

Update value in column based on column count in another table with mysql

From Dev

Select latest value of another column based on a comparison between unique keys

From Dev

Codeigniter, mysql, select_max... Insert when different column value

From Dev

Select max,min, last value from a column in mysql table

From Dev

Update column in one table based on value in another table in mysql

From Dev

Update column in one table based on value in another table in mysql

From Dev

Select COLUMN name from one table based on VALUE from another in mySQL?

From Dev

unique value based on another column value?

From Dev

Select rows with same id but different value in another column in a table

From Dev

MySQL SELECT incremental index based on value in a different column

From Dev

Mysql Query - Get row value based on column name in a different table

From Dev

Select specific row from mysql table based on a column value

From Dev

How to get max value of a column based on value of another column?

From Dev

HiveQL: Select value of column paired with Max(value) of another column

From Dev

Select unique values of one column with single non unique value of another table

From Dev

Create a column of unique identifiers based on another column in data.table

From Dev

MySQL update column value with max value from another column

From Dev

MySql Select Where Column Value Is Unique

From Dev

Select value based on two parameters : another column string value and max(date) with BigQuery

From Dev

SQL inner join on a column based on max value from another column

From Dev

R - Join on data.table, selecting a different column based on value of another column in row

From Dev

If duplicates exist, select the value based on another column

From Dev

Select max value of a column

From Dev

mysql insert unique values from one column to a column of another table

Related Related

  1. 1

    MySQL: Search based on a column, each unique value in another column and max time

  2. 2

    Get value based on max of a different column grouped by another column

  3. 3

    SELECT Mysql - Replacing value in one column based on another column

  4. 4

    select mysql column based on occurrence and value in another column

  5. 5

    Update value in a column based on another column in the same table in MYSQL

  6. 6

    Update value in column based on column count in another table with mysql

  7. 7

    Select latest value of another column based on a comparison between unique keys

  8. 8

    Codeigniter, mysql, select_max... Insert when different column value

  9. 9

    Select max,min, last value from a column in mysql table

  10. 10

    Update column in one table based on value in another table in mysql

  11. 11

    Update column in one table based on value in another table in mysql

  12. 12

    Select COLUMN name from one table based on VALUE from another in mySQL?

  13. 13

    unique value based on another column value?

  14. 14

    Select rows with same id but different value in another column in a table

  15. 15

    MySQL SELECT incremental index based on value in a different column

  16. 16

    Mysql Query - Get row value based on column name in a different table

  17. 17

    Select specific row from mysql table based on a column value

  18. 18

    How to get max value of a column based on value of another column?

  19. 19

    HiveQL: Select value of column paired with Max(value) of another column

  20. 20

    Select unique values of one column with single non unique value of another table

  21. 21

    Create a column of unique identifiers based on another column in data.table

  22. 22

    MySQL update column value with max value from another column

  23. 23

    MySql Select Where Column Value Is Unique

  24. 24

    Select value based on two parameters : another column string value and max(date) with BigQuery

  25. 25

    SQL inner join on a column based on max value from another column

  26. 26

    R - Join on data.table, selecting a different column based on value of another column in row

  27. 27

    If duplicates exist, select the value based on another column

  28. 28

    Select max value of a column

  29. 29

    mysql insert unique values from one column to a column of another table

HotTag

Archive