473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

MySQL UPDATE QUERY

Hi All, i need to know how to solve the error in the following query:

UPDATE table1 SET StopTime = NOW() WHERE StationId = (SELECT StationId
FROM table1 WHERE StopTime = "0000-00-00 00:00:00" AND Id = (SELECT
MAX(Id) FROM table1 WHERE UserName = "notconnected"))

The ERROR message is :

failed : You can't specify target table 'table1' for update in FROM
clause

What does this error mean, and how do i rectify it?

Thanks Wes

Jul 31 '06 #1
1 17523
we****@ispace.co.za wrote:
Hi All, i need to know how to solve the error in the following query:

UPDATE table1 SET StopTime = NOW() WHERE StationId = (SELECT StationId
FROM table1 WHERE StopTime = "0000-00-00 00:00:00" AND Id = (SELECT
MAX(Id) FROM table1 WHERE UserName = "notconnected"))

The ERROR message is :

failed : You can't specify target table 'table1' for update in FROM
clause

What does this error mean, and how do i rectify it?
The meaning is that MySQL has a problem using UPDATE and SELECT on the
same table in the same query.

Here's a alternative, using MySQL's multi-table updates:

UPDATE table1 AS t1
JOIN table1 AS t2 ON t1.StationId = t2.StationId
LEFT JOIN table1 AS t3 ON t2.Id < t3.Id
SET t1.StopTime = NOW()
WHERE t2.StopTime = '0000-00-00 00:00:00'
AND t2.UserName = 'notconnected'
AND t3.id IS NULL;

(I did not test that update. Please test it on a copy of your data and
make sure it does what you want.)

Regards,
Bill K.
Jul 31 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: joealey2003 | last post by:
Hi all... I have a column that contains names in upper case like: JOE, LAST NAME THOMAS JEFERSON MORS MIKE LEE ..
1
by: Paul | last post by:
Hello, I know mysql update queries can only utilize one index, making them slow in some cases. My update are taking about 3 hours, joining 2 tables on their primary keys. (table sizes about 1...
10
by: frizzle | last post by:
Hi there, I'm building a music site with a mysql backend. It has a many to many relational database. I use this to match music genres with certain artists, to maintain the possibility to add...
9
by: Jeff Gardner | last post by:
Greetings: I have an UPDATE query (php 5.1.6/mysql 5.0.24a on apache 2.2) that appears to execute with no errors (php,mysql, or apache) but the data in the "UPDATED" table doesn't change. I've...
3
by: PaulLFC | last post by:
Hi, I am writing a booking system for a Paintball company I work for here in the UK, and I seem to be having trouble with the mysql UPDATE code. Althogh no errors show, the mysql_affected_rows...
3
by: javakid | last post by:
Hi MySQL Gurus, I have a query as in following format: update tbl1, ( subquery )as tbl2 set tbl1.col1 = IFNULL(tbl2.col1,0) where tbl1.col2 = tbl2.col2Here, Actually i am returning a...
1
by: whitep8 | last post by:
Hi All, Can anybody see anything wrong with the following......its a mysql update query, however when ran (and adding vardump) i get nothing added to the database.... the error is......... ...
3
by: fishnfrogs | last post by:
Hi, I can't figure out why this isn't working. I'm trying to loop through an array and do a mysql update. However, it doesn't work. for($i = 0; $i < $len; ++$i) { $param = $array . '%';...
3
by: dgourd | last post by:
I am trying to run a really long Update query for my mysql database, but I keep getting a syntax error. $q = "UPDATE debator SET points = '" . mysqli_real_escape_string($link, $v) . "', win = '" ....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.