[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL select row with highest autonumber

Posted on 2011-04-21
10
Medium Priority
?
583 Views
Last Modified: 2012-05-11
experts.

I have a table with an autoincrement field (ID) and other fields (EMAIL).

please tell me how to select an EMAIL with the highest ID (newest record).

Thanks
0
Comment
Question by:thydzik
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35443207
SELECT MAX(ID) FROM your_table
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35443227
to get the email you would


SELECT MAX(ID),EMAIL FROM your_table
0
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 35443233
select email from table where id = (select max(id) from table)
0
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 17

Expert Comment

by:Daniel Reynolds
ID: 35443240
Select * FROM your_table where ID = (SELECT Max(ID) from your_table)
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35443251
Yeah, I was wrong, sorry wpcortes has the answer I believe
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35443279
or this:
select t.*
  from yourtable
 order by ID desc
 LIMIT 1

Open in new window

0
 
LVL 11

Author Comment

by:thydzik
ID: 35443308
sorry, guys for not being clear, I need to have a where condition for EMAIL, ie `EMAIL`='a@b.com'

so it needs to first select the email, and then the newest (highest ID) row.
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35443326

Try

SELECT *,MAX(ID) as max_id FROM your_table WHERE email = 'a@b.com';
0
 
LVL 16

Accepted Solution

by:
Walter Ritzel earned 2000 total points
ID: 35443327
select email from table t1 where id = (select max(id) from table where email = 'a@b.com')
0
 
LVL 11

Author Closing Comment

by:thydzik
ID: 35443352
thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question