SQL查询仅在字段等于value时选择最高ID

时间:2014-03-21 08:52:24

标签: mysql sql

表'部分':

enter image description here

我正在尝试创建执行以下操作的查询:

SELECT stop 
FROM section 
WHERE the id is the highest and the section is JPS_44-300-A-y10-1-1-1-H.

在上表中,结果将是1900HA080909。我怎么能用SQL做到这一点?

4 个答案:

答案 0 :(得分:2)

您可以使用LIMIT功能执行此操作:

SELECT stop 
FROM section 
WHERE section='JPS_44-300-A-y10-1-1-1-H'
ORDER BY ID DESC
LIMIT 1;

答案 1 :(得分:1)

请尝试:

select * From YourTable a
where a.ID=(select MAX(ID) from YourTable b where b.Section=a.Section)

答案 2 :(得分:0)

SELECT stop FROM section
INNER JOIN (SELECT MAX(ID) FROM section WHERE section = 'JPS_44-300-A-y10-1-1-1-H') AS Tbl
 ON section.ID = Tbl.ID

答案 3 :(得分:0)

尝试那样

SELECT a.stop FROM section a WHERE a.id IN (
     SELECT MAX(id) FROM section b
    WHERE b.section = 'JPS_44-300-A-y10-1-1-1-H'
)