error 1093 mysql update query - mysql

so I am trying to run this sql:
UPDATE creature_template
SET
subname = "Utgarde Keep Heroics",
Health_mod = Health_mod * 45,
mindmg = mindmg * 100,
maxdmg = maxdmg * 100,
Armor_mod = armor_mod * 4
WHERE entry IN (
SELECT difficulty_entry_1
FROM creature_template
WHERE entry IN (
SELECT id FROM creature WHERE map = 574
)
);
But I am getting this error:
[Err] 1093 - You can't specify target table 'creature_template' for update in FROM clause
How am I supposed to run it?

You cannot update the same table that you use in the SELECT part in MySQL. You'll need to use a sub-query like below to create a temporary table in the nested sub-query and it will not count as the same table you are updating:
UPDATE creature_template
SET
subname = "Utgarde Keep Heroics",
Health_mod = Health_mod * 45,
mindmg = mindmg * 100,
maxdmg = maxdmg * 100,
Armor_mod = armor_mod * 4
WHERE entry IN (
SELECT difficulty_entry_1
FROM (creature_template
WHERE entry IN (
SELECT id
FROM creature ) as temp
WHERE map = 574
)
);
See also MySQL documentation on the UPDATE syntax
Hope this helps

Query syntax is wrong.
You can update the same table that is referenced in selcting the entry.
Check this link:
MySQL DOCUMENT FOR THIS:

Related

MySql Error 1093 trying to UPDATE using two databases and two tables

DataBase_1 has a list of users with their vBulletinId
DataBase_2 has the vBulletin users with their vBulletinId and UserGroupId
I want to find all vBulletin users from Database_2, who are NOT in Database_1, and change their UserGroupId to 15
UPDATE database_2.user
SET
usergroupid = 15
WHERE
(
SELECT UserId
FROM database_2.user
WHERE UserId NOT IN
(
SELECT vBulletinId
FROM database_1.Users
)
);
Currently I am getting a "You can't specify target table 'user' for update in FROM clause"
Any help would be great. thanks.
=== SOLUTION ===
A simplified solution that worked.
UPDATE database_2.user
SET
usergroupid = 15
WHERE userid NOT IN
(
SELECT vBulletinId FROM database_1.Users
);
UPDATE database_2.user
LEFT JOIN database_1.Users ON database_2.user.UserId = database_1.Users.vBulletinId
SET database_2.user.usergroupid = 15
WHERE database_1.Users.vBulletinId IS NULL
or
UPDATE database_2.user
SET database_2.user.usergroupid = 15
WHERE NOT EXISTS ( SELECT NULL
FROM database_1.Users
WHERE database_2.user.UserId = database_1.Users.vBulletinId )

Multiply in update subquery select

I'm trying to update a table based on 2 select subquery that will be multipied to produce the value for Harga column
here is my code :
UPDATE bahanmakanan
SET Harga = (SELECT HargaSatuan from detail_bahanmakanan
WHERE IDBahanMakanan = "BM01")* (SELECT jumlah from bahanmakanan
WHERE IDBahanMakanan = "BM01")
WHERE IDBahanMakanan = "BM01" ;
The error message return
Error Code: 1093. You can't specify target table 'bahanmakanan' for update in FROM clause
you can simply do this using JOIN,
UPDATE bahanmakanan a
INNER JOIN detail_bahanmakanan b
ON a.IDBahanMakanan = b.IDBahanMakanan
SET a.Harga = a.jumlah * b.HargaSatuan
WHERE a.IDBahanMakanan = 'BM01'
Please do backup first your database before executing the statement.
Try this:
UPDATE bahanmakanan as t1
JOIN detail_bahanmakanan as t2 USING(IDBahanMakanan)
SET t1.Harga = t2.HargaSatuan * t1.jumlah
WHERE IDBahanMakanan = "BM01";

Update a row with data from another row within the same table

UPDATE user SET
tw_oauth_token=(SELECT tw_oauth_token FROM user WHERE id=27),
tw_oauth_token_secret = (SELECT tw_oauth_token_secret FROM user WHERE id=27),
tw_user_id = (SELECT tw_user_id FROM user WHERE id=27),
handler = (SELECT handler FROM user WHERE id=27),
merged=1 WHERE id=26
The idea is to select data from user table where id = 27 and update the same table where id = 26.
I am having the following error:
#1093 - You can't specify target table 'user' for update in FROM clause
Any help would be appreciated. Thanks
UPDATE user a
CROSS JOIN user b
SET a.tw_oauth_token = b.tw_oauth_token,
a.tw_oauth_token_secret = b.tw_oauth_token_secret,
a.tw_user_id = b.tw_user_id,
a.handler = b.handler,
a.mrged = 1
WHERE a.ID = 26 AND
b.ID = 27
Why don't you try to create a virtual table based on your table then update the user table with SELECT statements on that View. Something like this:
CREATE VIEW view_user AS
SELECT *
FROM user;
And then use the view_user in the update.

Update table mysql and if value is 10 update another table

I've following sql to update results table:
$mysqli->query("UPDATE results
SET result_value = IF('$logo_value' - result_tries < 0 OR '$logo_value' - result_tries = 0, 1, '$logo_value' - result_tries)
WHERE logo_id = '$logo_id'
AND user_id = '$user_id'
AND result_value = 0");
In the same sql command is it possible to update another table based on result_value?
if result_value = 10
Update users SET user_hints = user_hints +1 WHERE user_id = '$user_id'
How would I incorporate this into sql syntax above?
Long way I can think of is to select this value get it into php variable. And than do another update based on php variable value... But this seems long and tedious
This is a long shot (not tested) but how about:
$mysqli->query("UPDATE results, users
SET result_value =
IF('$logo_value' - results.result_tries < 0 OR
'$logo_value' - results.result_tries = 0,
1, '$logo_value' - result_tries),
users.user_hints =
IF(results.result_value >= 10,
users.user_hints + 1, users.user_hints)
WHERE results.logo_id = '$logo_id'
AND results.user_id = '$user_id'
AND results.user_id = users.user_id
AND results.result_value = 0");
If both tables have some of the same column names, of course, youll have to specify which table (like results.user_id -or- users.user_id)

Complex UPDATE query in SQL Server 2008, rewrite from Oracle 10g

I need to re-write the following Oracle 10g query to work in SQL Server 2008
It's an update query, where some field are retrieved from a SELECT and some are given (from code).
UPDATE "BMAN_SQL"."CELLS_GLIST"
SET ("GLIST_ID", "GLIST_VALUE_ID") = (
SELECT "GLIST_ID", "GLIST_VAL_ID"
FROM "BMAN_SQL"."GLISTS_VAL_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_CODE"='X')
),
"SESSION_ID" = 1553245736,
"USER_ID" = 13
WHERE EXISTS ( SELECT * FROM ... )
Note that I need to use the UPDATE SET ... WHERE EXIST ... structure for compatibility with Oracle (query are automatically built by a QueryBuilder class for each specific DBMS).
I also cannot write:
UPDATE "BMAN_SQL"."CELLS_GLIST"
SET ("GLIST_ID", "GLIST_VALUE_ID", "SESSION_ID", "USER_ID") = (
SELECT "GLIST_ID", "GLIST_VAL_ID", 1553245736, 13
FROM "BMAN_SQL"."GLISTS_VAL_UOR"
WHERE ("UOR_ID"=3)
AND ("GLIST_CODE"='X')
)
WHERE EXISTS ( SELECT * FROM ... )
because (as per this old thread Oracle "Cannot update to NULL") it returns an error if the SELECT does not fetch any record.
Thanks in advance!
You need to join update query in this case. Please see syntax below. <Your condition here> Replace this section with your join condition here.
UPDATE m
SET
GLIST_ID = r.GLIST_ID
,GLIST_VALUE_ID = r.GLIST_VAL_ID
, SESSION_ID= 1553245736
, USER_ID = 13
from BMAN_SQL.CELLS_GLIST m
inner join BMAN_SQL.GLISTS_VAL_UOR r on <Your condition here>
WHERE
r.UOR_ID=3 AND (r.GLIST_CODE='X') AND
EXISTS ( SELECT * FROM ... )
EDIT
UPDATE BMAN_SQL.CELLS_GLIST
SET
GLIST_ID = (SELECT TOP 1 GLIST_ID FROM BMAN_SQL.GLISTS_VAL_UOR WHERE (UOR_ID=3) AND (GLIST_CODE='X'))
,GLIST_VALUE_ID = (SELECT TOP 1 GLIST_VAL_ID FROM BMAN_SQL.GLISTS_VAL_UOR WHERE (UOR_ID=3) AND (GLIST_CODE='X'))
,SESSION_ID = 1553245736
,USER_ID = 13
WHERE EXISTS ( SELECT * FROM ... )