SQL-如何查找列中的最高编号? [英] SQL - How to find the highest number in a column?

查看:90
本文介绍了SQL-如何查找列中的最高编号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在客户"表中有以下数据:(仅此而已)

Let's say I have the following data in the Customers table: (nothing more)

ID   FirstName   LastName
-------------------------------
20   John        Mackenzie
21   Ted         Green
22   Marcy       Nate

什么样的SELECT语句可以在ID列中获得22号?

What sort of SELECT statement can get me the number 22, in the ID column?

我需要执行类似的操作来生成唯一的ID.当然,我可以让系统通过自动递增来执行此操作,但是那我将如何获取自动生成的ID?

I need to do something like this to generate a unique ID. Sure I can let the system do this via auto-increment, but then how would I get the auto generated ID?

我想到了SELECT ID FROM Customers并计算返回的行,但这似乎效率很低,在这种情况下,尽管我需要唯一的ID 23,但它会错误地返回"3".

I thought of SELECT ID FROM Customers and counting the rows returned but this seems horribly inefficient, and in this case, it will incorrectly return "3", though I need a unique ID of 23.

推荐答案

您可以

SELECT MAX(ID) FROM Customers;

这篇关于SQL-如何查找列中的最高编号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆