如果仅满足条件,如何使用 SQL 语法从 MySQL 表中选择最后一条记录 [英] How to select the last record from MySQL table using SQL syntax if only a condition is meet
本文介绍了如果仅满足条件,如何使用 SQL 语法从 MySQL 表中选择最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据库中有四个字段
I have four fields in my db namely
Id(auto increment),
dept_id,
mat_code,
topic
如果满足条件,我想检索数据库中的最后一条记录.我使用 dept_id 作为条件.
I want to retrieve the last record in the database if a condition is meet. Am using dept_id for the condition.
推荐答案
正常的方法是将结果按 DESC 顺序排序,并将结果集限制为 1 行
The normal method is to order the result in DESC order and LIMIT the result set to 1 row
SELECT Id, dept_id, mat_code, topic
WHERE dept_id = 'something'
ORDER BY Id DESC
LIMIT 1
这篇关于如果仅满足条件,如何使用 SQL 语法从 MySQL 表中选择最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文