Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I Update the last coloumn in the database?

Status
Not open for further replies.

maverik59

Programmer
Oct 30, 2002
67
GB
I need to update the last coloumn in the database in SQL I could use sub queries ie-
UPDATE usertrack SET heading_to = "123" WHERE unique_id=
(select max(unique_id) from usertrack)

Is there a way round this in mysql?
Cheers

 
If you're using an auto_increment field, you can use the mysql command LAST_INSERT_ID() to identify the last row that has been created. --
How can you be in two places at once when you're not anywhere at all?
 
Yes I have been looking at this, but it won't let me update the last coloumn still. I've been trying something like this.->

Select max(last_insert_id(unique_id)) as Simon
from usertrack
Update usertrack set heading_to="si"
where unique_id=simon
 
In rereading last_insert_id(), it will only ork if you've inserted a value in the current connection. And since I don't think MySQL supports nested selects, you'd have to get the last ID, then use that to update the database separately. Your application might have to do some work and perform two queries:

e.g. in PHP
$query=&quot;select max(id) from <table>&quot;;
$res=mysql_query($query);
$max_id=mysql_fetch_row($res);
$query=&quot;update <table> set <column>=<something> where id=$max_id&quot;;

It's a kludge... and only to give you an idea of what I mean. --
How can you be in two places at once when you're not anywhere at all?
 
Select @simon:=max(last_insert_id(unique_id))
from usertrack
Update usertrack set heading_to=&quot;si&quot;
where unique_id=@simon
______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I had thought last_insert_id() only worked in the current connection. --
How can you be in two places at once when you're not anywhere at all?
 
Setting a user defined variable in mysql will keep the variable for the duration of the session, possibly a pain if you have a permanent connection from PHP tho. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top