1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

3

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)
Sari Alalem
  • 870
  • 7
  • 18
1

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 :)

Ray
  • 40,256
  • 21
  • 101
  • 138
  • 1
    The `HAVING` clause seems overkill in this query, why not just `SELECT MAX(date) WHERE magazin = %s` in the inner query? – André Laszlo Mar 02 '14 at 15:50
  • @AndréLaszlo yes, that's what I think, going to look for another better solution, but the above would work. – Ray Mar 02 '14 at 15:51