SQL query error - Selecting from table with conditions - mysql

I'm currently trying to figure out how to make a query that fits my needs. It has to select a modelid from the creature table where the id = 553 and update the creature_template table column displayid with the id it has selected but only if entry = 100098.
I have written this SQL:
UPDATE `creature_template`
SET `modelid1` = (SELECT `modelid`
FROM `creature`
WHERE `map` = 553)
WHERE `entry` = 100098
However I get this error when I add it to my database:
Subquery returns more than 1 row.

Your subquery returns more than one value. That means, there are more than one rows in creature with map field equals 553. This query will update only one of that rows.
UPDATE `creature_template`
SET `modelid1` = (SELECT `modelid`
FROM `creature`
WHERE `map` = 553
LIMIT 1
)
WHERE `entry` = 100098
But if you need to update all rows with map=553, you should use IN cause:
UPDATE `creature_template`
SET `modelid1` IN (SELECT `modelid`
FROM `creature`
WHERE `map` = 553
)
WHERE `entry` = 100098

Related

Update non-null field with possibly null values

In the query below:
update collect_irc_deploy c
set hid = (select id
from auth_hierarchy
where fqdn = (select location
from reserve
where id=c.rid
)
)
where hid = 0 and rid is not null
the subquery select id from auth_hierarchy where fqdn = (select location from reserve where id = c.rid) may return NULL while the field hid is NOT NULL.
How can I modify the statement so that if the subquery returns NULL that data item is skipped instead of failing the entire execution?
You can use update...join syntax to ensure only joined rows are updated:
update collect_irc_deploy
join reserve on reserve.id = collect_irc_deploy.rid
join auth_hierarchy on auth_hierarchy.fqdn = reserve.location
set collect_irc_deploy.hid = auth_hierarchy.id
where collect_irc_deploy.hid = 0 and collect_irc_deploy.rid is not null
Use UPDATE IGNORE solved my problem. But it will generate warning messages.

Update data from one table to another in vertical table

I understand basically the concepts of UPDATE to use data in one table to update another similar table. However the table data I have to update to is arranged in a 'vertical' manner as opposed to the 'horizontal' manner of the input table. This query works if I limit it to just one record :
SELECT #userid:=user_id,#club:=CLUB, #financialdate:=FINANCIALDATE
FROM wpty_sa_tmp_update where user_id = 1;
UPDATE wpty_cimy_uef_data SET VALUE = #financialdate WHERE user_id = #userid and field_id = 16;
UPDATE wpty_cimy_uef_data SET VALUE = #club WHERE user_id = #userid and field_id = 8;
If I remove the WHERE user_id clause, it does not update .. what am I missing?
Obviously I can't create a join of any sort because the 2 tables don't share a common ID or key
cheers
You could actually do this from a single update statement:
UPDATE wpty_cimy_uef_data wc
INNER JOIN wpty_sa_tmp_update ws
ON wc.user_id = ws.user_id AND ws.user_id = 1
SET
VALUE = CASE field_id WHEN 16 THEN ws.FINANCIALDATE
WHEN 8 THEN ws.CLUB END
WHERE
field_id IN (8, 16);

mySQL UPDATE WHERE with subquery gives error

I want to update a table with a subquery and always get an error.
Now i made a very simplified version (which makes not much sense but shows my error)
UPDATE a_test SET categoryID = '2956' WHERE id IN (
(
SELECT id from a_test
)
)
This ends in this error:
#1093 - Table 's_articles_categories' is specified twice, both as a target for 'UPDATE' and as a separate source for data
Why do i get this error?
When i use aliasses for the table a_test i get the same error.
This is the full query i want to use with the same error:
UPDATE s_articles_categories SET categoryID = '2956' WHERE id IN
(
SELECT s_articles_categories.id FROM `s_articles`
LEFT JOIN s_articles_categories ON s_articles.id = s_articles_categories.articleID
WHERE s_articles_categories.categoryID NOT IN (
SELECT id FROM s_categories
WHERE s_categories.id NOT IN (SELECT parent FROM s_categories WHERE parent IS NOT null GROUP BY parent)
)
)
One solution to the simplified query is to wrap the subquery inside another subquery:
UPDATE a_test
SET categoryID = '2956'
WHERE id IN (SELECT id FROM (SELECT id FROM a_test) x );
This trick forces MySQL to materialize the subquery on a_test, so that the values coming from the subquery aliased as x are not affected as the update proceeds.

MySQL Update subquery table is specified twice

I'm trying this subquery to decrease 1 in my quantity every time I execute this query. I think it will work on SQL-Server but why isn't it working on MySQL. I'm getting this error:
Table 't1' is specified twice, both as a target for 'UPDATE' and as a separate source for data
here's my code
Update tblbooks AS t1
set t1.Quantity = (Select t2.Quantity-1
from tblbooks AS t2
where t2.BookId = 123)
where t1.BookId = 123
You don't need the subquery there - you can reassign a calculation on a column back to the same column:
UPDATE tblbooks
SET Quantity = Quantity - 1
WHERE BookId = 123

Multi Subquery count function unable to take the stress of multiple selects

This has me stumped.
MySQL
UPDATE sets SET sets.current_count = (SELECT COUNT(leads_auto.set_id) AS current_count FROM leads_auto WHERE leads_auto.set_id = (SELECT sets.set_id AS setID FROM sets WHERE sets.on_off = 0)) WHERE sets.on_off = 0
Seems right doesn't it? Update the record current_count with the total number of rows in leads_auto which have the set_id value of the set_id of the sets table where the value of its on_off column is 0.
yet I get this error
#1093 - You can't specify target table 'sets' for update in FROM clause
I looked around and someone mentioned that it has to do with the operation being cyclic?
Create a temporary table for the result of SET
UPDATE sets
SET sets.current_count =
(
SELECT COUNT(leads_auto.set_id) AS current_count
FROM leads_auto
WHERE leads_auto.set_id =
(
SELECT set_id
FROM
(
SELECT sets.set_id AS setID
FROM sets
WHERE sets.on_off = 0
) c
)
)
WHERE sets.on_off = 0