我对sql语句感到困惑,我需要你的帮助。
以下是我的表格(tblfood)内容:
ID Food_Name Main_Ingredient Drink
1 Ham n Coke Ham Coke
2 Cheese n Tea Cheese Ice Tea
3 Ham n Coke Ham Coke
4 Ham n Coke Ham Coke
5 Chicken Sandwich Chicken None
6 Chicken Sandwich Chicken None
7 Cheese n Tea Cheese Ice Tea
8 Chicken n Coke Chicken Coke
9 Chicken Sandwich Chicken None
10 Cheese n Tea Cheese Ice Tea
我想获得最后一次输入ID
的{{1}} Food_Name
,其中包含Chicken Sandwich
,这是10.如果我最后输入Food_Name
Chicken n Coke
},它会显示9。
我的第一次尝试是这样的:
Select id
from tblfood
where Food_Name='Chicken Sandwich';
但是使用这段代码,我相信它会显示Food_Name等于Chicken Sandwich的所有ID。
我应该如何仅显示最后输入的ID?
答案 0 :(得分:1)
SELECT ID
FROM tblfood
WHERE Food_Name = 'Chicken Sandwich'
ORDER BY ID DESC
LIMIT 1
答案 1 :(得分:1)
SELECT id
FROM tblfood
WHERE Food_Name='Chicken Sandwich'
ORDER BY id desc limit 1;
答案 2 :(得分:1)
如果你需要多个你需要依赖Order By子句解决方案,考虑到你想要一个id不是多个,那么选择max id。
SELECT MAX(id)
FROM tblfood
WHERE Food_Name='Chicken Sandwich';
答案 3 :(得分:0)
请尝试
SELECT TOP 1 *
FROM tblfood
WHERE Food_Name='Chicken Sandwich'
ORDER BY ID DESC
答案 4 :(得分:0)
Select id from tblfood where Food_Name='Chicken Sandwich' and dateofentry in(select max(dateofentry) from tblfood) ';