Skip to content
Advertisement

Problems with SQL syntax? MAX not working

$query="SELECT title, max(postid) FROM post";

postid is a increasing numeric value (1,2,3...). At the moment, the highest value is 49, but It’s pulling up 1. It seems to be ignoring the MAX statement.

otherwise everything else is working great.

Advertisement

Answer

That’s not valid syntax, which engine is running it?

You either need:

 SELECT title, max(postid) FROM post GROUP BY title;

to get multiple records, one for each title, showing the max postid for each title, or

SELECT max(postid) FROM post

to get the single max postid from the table.

If you want the highest postid and the title that goes with it, you need

 SELECT TOP 1 title, postid FROM post ORDER BY postid DESC

or

 SELECT title, postid FROM post ORDER BY postid DESC LIMIT 1

depending on your SQL engine.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement