Jump to content

Weird MySql query


rohitbanerjee

Recommended Posts

So suppose I have a mysql table like the following

 

          Col1  Col2  Col3

Row1: A      10    2012-3-21 13:20:09

Row2: A      12    2012-3-20 12:04:09

Row3: A      5      2012-3-20 12:03:03

 

I need a query which will fetch me Row1 and Row2 given that I am searching for A, if the query sees that two rows occur on the same date, it should return the latest one. Any thoughts?

Link to comment
https://forums.phpfreaks.com/topic/260729-weird-mysql-query/
Share on other sites

Pretty sure this will error out all over the place, but I would try something like:

SELECT Col1, Col2, Col3
FROM table1
WHERE(
Col1 = 'A'
AND
DATE(Col3) IN 
(
SELECT DATE(Col3) as date_range FROM table 1 AS dt_table1 WHERE 
  (
  TIME(td_table1.Col3) IN 
    (
    SELECT MAX(TIME(Col3)) as max_time FROM table1 as tt_table1 GROUP BY DATE(tt_table1.Col3)
    )
   )
  )
)
)

.....actualy, no I woudln't, I'd already have a date column in the table that I could use for this lookup.

Link to comment
https://forums.phpfreaks.com/topic/260729-weird-mysql-query/#findComment-1336379
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.