2

Recently I started working on a project. In which I want to fetch the last record inserted in the database.

Query is:

SELECT c.lat, c.lng, LAST(v.count) 
FROM camera_camera c , camera_cameravehiclecount v 
WHERE c.id = v.cameraid 

I am using the LAST() aggregate function but I am getting this error:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(v.count) from camera_camera c , camera_cameravehiclecount v WHERE c.id = v.came' at line 1

I cross checked the syntax from w3school but still I am getting this error. I am using DB4free as my online database.

2
  • 1
    What exactly are you trying to achieve? Commented May 28, 2016 at 16:19
  • 1
    I just want to get the last inserted value of "count" from the table where id,s are same. Commented May 28, 2016 at 16:22

2 Answers 2

2

The w3 page suggests a workaround, since LAST isnt in mysql. http://www.w3schools.com/sql/sql_func_last.asp

You can try this to get the same thing:

SELECT c.lat, c.lng, v.count
FROM camera_camera c , camera_cameravehiclecount v 
WHERE c.id = v.cameraid 
ORDER BY v.count DESC 
LIMIT 1
Sign up to request clarification or add additional context in comments.

Comments

1

First, you need to learn to use proper JOIN syntax. Simple rule: Never use commas in the FROM clause. Always use explicit JOIN.

Second, you need a column to specify what last is. Say it is the id on the vehicle count table. Then you would use order by and limit:

SELECT c.lat, c.lng, LAST(v.count) 
FROM camera_camera c JOIN
     camera_cameravehiclecount v 
     ON c.id = v.cameraid 
ORDER BY v.id DESC
LIMIT 1;

Note: ORDER BY v.count DESC is not appropriate, unless you know that that column is only increasing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.