user3303717
user3303717

Reputation: 55

Select multiple rows with max date

I'm new to mysql so please help me out with this.

I have a table containing the following columns:

nr  |   date   |  hour  |  user  |  shop  |  brand  |  categ  | product | price | promo
183   02/03/14   17:06    cristi    186     brand1    categ 1    prod 1    299     no
184   02/03/14   17:06    cristi    186     brand2    categ 2    prod 2    399     yes
184   01/03/14   17:06    cristi    186     brand3    categ 3    prod 3    199     no

The query that I use is

SELECT * 
FROM evaluari 
WHERE magazin = %s HAVING MAX(data)

Where "s" is the shop ID (186).

but that return only the first row containing 02/03/14 date. How can I show both/all rows containing the same max date?

Upvotes: 1

Views: 90

Answers (2)

Sari Alalem
Sari Alalem

Reputation: 870

Try not to name colums with reserved words like "date" it might cause you problems. You can do what you want like this:

SELECT * FROM evaluari WHERE magazin = 186 AND date = (SELECT MAX(date) from evaluari WHERE magazin = 186)

Upvotes: 3

Ray
Ray

Reputation: 41408

Probably, not optimal, but at first swing, you could do this

 SELECT * FROM evaluari 
   where date IN (SELECT date FROM evaluari WHERE magazin = %s HAVING MAX(date)) 
          AND magazin = %s;

In fact, this really rubs me as nasty... going to try to figure something smoother. Stay tuned :)

Upvotes: 1

Related Questions