简体   繁体   中英

Select the value of max id from same date

I have a table with some data, now what i want to do is select the value of the row that has the current date and max id.

CREATE TABLE `pa` (
  `pd` int(100) NOT NULL,
  `ad` int(100) NOT NULL,
  `rd` int(11) NOT NULL,
  `p_a_d` datetime NOT NULL,
  `value` float DEFAULT NULL,
  `l_l_d` datetime NOT NULL,
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Now what I want is to select the value of the max(pd) for the same ad when the p_a_d is has current date. This is what I am trying and I have to use that query in a view. What I am trying is

(SELECT `pa`.value  FROM pa  WHERE pd
  =(SELECT MAX(`pa`.`pd`)) AND DATE(p_a_d) =
  DATE(CURRENT_DATE))  AS y

Tell me if it helps with second part of your request

SELECT `pa`.value,`pa`.`pd`   FROM pa  
 WHERE 
 DATE(p_a_d) =  DATE(CURRENT_DATE)
order by `pa`.`pd` desc
limit 1

try that :

 SELECT `pa`.value  
 FROM pa  
 WHERE pd in (SELECT MAX(`pd`) from pa) 
 AND DATE(p_a_d) = DATE(CURRENT_DATE)

DEMO

SELECT `pa`.value,ad,`l_l_d`  FROM pa  
WHERE pd
in (SELECT MAX(`pd`) from pa) 
AND DATE(p_a_d) =
DATE(CURRENT_DATE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM