Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only rows by join tables max value

Tags:

sql

join

I want to select only the row which has max b.enddate for u.classno, u.userno. But it doesn't work.

select u.classno, u.userno, b.enddate
from libUser u
join book b on b.id = u.bookid
group by u.classno, u.userno
having b.enddate=max(b.enddate) //doesn't works
like image 493
karlkeller Avatar asked Sep 09 '13 08:09

karlkeller


People also ask

How do you SELECT a row with maximum value?

MySQL select the row with maximum value in a column : MAX() function. This section will help us learn how to get the maximum value for a column and get the record details corresponding to it. Let us start by creating a table sales_details followed by inserting some records to it.

How do I SELECT a maximum value in a column in SQL?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

How do I find the highest record value in SQL?

To find the maximum value of a column, use the MAX() aggregate function; it takes a column name or an expression to find the maximum value. In our example, the subquery returns the highest number in the column grade (subquery: SELECT MAX(grade) FROM student ).


2 Answers

Here is an excellent article in the official MySQL documentation, but only standard SQL is used there, so it can be applied to whatever RDBMS you are using.

The Rows Holding the Group-wise Maximum of a Certain Column

Task: For each article, find the dealer or dealers with the most expensive price.

This problem can be solved with a subquery like this one:

SELECT article, dealer, price FROM   shop s1 WHERE  price=(SELECT MAX(s2.price)               FROM shop s2               WHERE s1.article = s2.article); 

The preceding example uses a correlated subquery, which can be inefficient (see Section 13.2.10.7, “Correlated Subqueries”). Other possibilities for solving the problem are to use an uncorrelated subquery in the FROM clause or a LEFT JOIN.

Uncorrelated subquery:

SELECT s1.article, dealer, s1.price FROM shop s1 JOIN (   SELECT article, MAX(price) AS price   FROM shop   GROUP BY article) AS s2   ON s1.article = s2.article AND s1.price = s2.price; 

LEFT JOIN:

SELECT s1.article, s1.dealer, s1.price FROM shop s1 LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price < s2.price WHERE s2.article IS NULL; 

The LEFT JOIN works on the basis that when s1.price is at its maximum value, there is no s2.price with a greater value and the s2 rows values will be NULL.

like image 178
fancyPants Avatar answered Sep 25 '22 05:09

fancyPants


Whats wrong with:

select u.classno, u.userno, MAX(b.enddate) from libUser u join book b on b.id = u.bookid group by u.classno, u.userno 
like image 21
Filip Avatar answered Sep 25 '22 05:09

Filip