Mysql - Return max value from joined table

Ciprian

I m not sure how to return the highest value from the joined table. There are currently 4 items in the table but the query is only returning one. The MAX(b.bid) is breaking the query.

SELECT i.id, 
 i.user_id, 
 i.item_title, 
 i.item_description, 
 i.item_condition, 
 i.active, 
 i.add_date, 
 MAX(b.bid)
FROM items i
LEFT JOIN bids b
 ON i.id = b.item_id
WHERE i.user_id = ? AND i.active = 1 
ORDER BY i.id DESC
pala_

If you want the max(bid) per item, you're going to need to group your query by item.

SELECT i.id, 
 i.user_id, 
 i.item_title, 
 i.item_description, 
 i.item_condition, 
 i.active, 
 i.add_date, 
 MAX(b.bid)
FROM items i
LEFT JOIN bids b
 ON i.id = b.item_id
WHERE i.user_id = ? AND i.active = 1 
GROUP BY i.id
ORDER BY i.id DESC

This result depends on all the other fields being the same for each bid, which it looks like it should be. This is a valid use of the mysql extension to group by. Otherwise you'd need to specify each field in the select in the group by clause.

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 update query on a single table with value from joined table

From Dev

sqlite: update TableA with a max() from a joined table

From Dev

Getting max date from a joined table with group by

From Dev

MYSQL select max date from joined tables

From Dev

SELECT min max and max min value from a part of a table in MySQL

From Dev

MySQL function return value from row in table

From Dev

Getting highest column value from joined table

From Dev

Fetch Column names from a joined mysql table

From Dev

Return name of object from table with max value lua

From Dev

MySQL return Null = 0 on joined table with filter on one table

From Dev

How do you pull all data from a joined table when a related record contains a given value in MySQL?

From Dev

MySQL JOIN table based on MAX(date) in main table, and MAX(id) in joined table with LIMIT

From Dev

Update a mysql table column with sum of joined table columns value?

From Dev

jOOQ Return a POJO, and ID from from joined table

From Dev

How to return null value if no record found on joined table

From Dev

Trying to correct MySQL query to return desired result w/ joined table

From Dev

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

From Dev

mysql query get max and distinct value from table

From Dev

Get Max value, Distinct values from Table in MySQL

From Dev

How to select distinct max value from multiple join table MySQL

From Dev

Return table from mySQL with a selected option from the database value

From Dev

MySql limiting only first table from joined table

From Dev

Laravel 5.4 - Return single row from joined table

From Dev

MySQL averaging joined table

From Dev

MySQL count on joined table

From Dev

Get max value from table

From Dev

How to select a minimal value from a joined table with JPA?

From Dev

Most recent distinct record from a joined MySQL table

From Dev

How to retrieve min price via mysql query from a joined table

Related Related

  1. 1

    MySQL update query on a single table with value from joined table

  2. 2

    sqlite: update TableA with a max() from a joined table

  3. 3

    Getting max date from a joined table with group by

  4. 4

    MYSQL select max date from joined tables

  5. 5

    SELECT min max and max min value from a part of a table in MySQL

  6. 6

    MySQL function return value from row in table

  7. 7

    Getting highest column value from joined table

  8. 8

    Fetch Column names from a joined mysql table

  9. 9

    Return name of object from table with max value lua

  10. 10

    MySQL return Null = 0 on joined table with filter on one table

  11. 11

    How do you pull all data from a joined table when a related record contains a given value in MySQL?

  12. 12

    MySQL JOIN table based on MAX(date) in main table, and MAX(id) in joined table with LIMIT

  13. 13

    Update a mysql table column with sum of joined table columns value?

  14. 14

    jOOQ Return a POJO, and ID from from joined table

  15. 15

    How to return null value if no record found on joined table

  16. 16

    Trying to correct MySQL query to return desired result w/ joined table

  17. 17

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

  18. 18

    mysql query get max and distinct value from table

  19. 19

    Get Max value, Distinct values from Table in MySQL

  20. 20

    How to select distinct max value from multiple join table MySQL

  21. 21

    Return table from mySQL with a selected option from the database value

  22. 22

    MySql limiting only first table from joined table

  23. 23

    Laravel 5.4 - Return single row from joined table

  24. 24

    MySQL averaging joined table

  25. 25

    MySQL count on joined table

  26. 26

    Get max value from table

  27. 27

    How to select a minimal value from a joined table with JPA?

  28. 28

    Most recent distinct record from a joined MySQL table

  29. 29

    How to retrieve min price via mysql query from a joined table

HotTag

Archive