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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.