mySQL UPDATE WHERE with subquery gives error - mysql

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.

Related

How do I update a table that is being queried within the query itself?

I'm trying to update a table through a query, the query itself is created using the results of queries to other tables (one of which is the table we are updating)...
However, I keep getting an error when I try to execute the query... After some research I found out I have to encase the inner query with a SELECT * FROM ()... but this doesn't seem to have worked...
I can't figure out how to bypass this error with my MySQL Query...
This is the error I'm getting...
[Err] 1093 - You can't specify target table 'players' for update in
FROM clause
This is my query...
DELETE FROM players WHERE name='Henry' AND player_group_id IN
(
SELECT id FROM playergroups WHERE player_set_id=
(
SELECT id FROM playersets WHERE player_name=
(
SELECT name FROM
(
SELECT name FROM players WHERE player_group_id=
(
SELECT id FROM playergroups WHERE player_set_id=
(
SELECT id FROM playersets WHERE player_name='Henry'
)
)
) AS P1
)
)
);
use joins, avoid to much use of nested statements
You should really find a better way to navigate though the tables, but where exists is a good way to around that error. Or you could stage off the data and delete based of PIs.
DELETE FROM players A
WHERE name = 'Henry'
AND WHERE EXISTS
(
SELECT 1
FROM playersgroup B
WHERE A.players_group_id = B.players_group_id
)
ETC.. ETC.. Depending on how many tables you need to reference.

MySQL update column value with max value from another column

I have a database with 2 tables:
"services" and "service_performance"
Those 2 tables have a SERVICE_ID column.
In "services" the SERVICE_ID values are unique (each service has a single ID/entry).
In "service_performance" there is an AVERAGE_MEMORY column with multiple entries per service_id
I am trying to update the MAX_VALUE column in "services" table with the highest AVERAGE_MEMORY value taken from the "service_performance" table.
I know my query is wrong because it throws an error:
1054 - Unknown column 'service_performance.SERVICE_ID' in 'where clause'
While 'service_performance.SERVICE_ID' does exist.
Here is my query:
update _services
set MAX_VALUE = (SELECT MAX(AVERAGE_MEMORY) AS SERVICE_ID FROM service_performance)
where exists
(select *
from services
where `services`.`SERVICE_ID` = `service_performance`.`SERVICE_ID`);
You should find that this version works in MySQL:
update services s join
(select service_id, MAX(AVERAGE_MEMORY) as maxmem
from service_performance
group by service_id
) sp
on s.service_id = sp.service_id
set s.MAX_VALUE = sp.maxmem;
Your version would work if it had the right table name in the where clauses:
update services
set MAX_VALUE = (SELECT MAX(AVERAGE_MEMORY) AS SERVICE_ID
FROM service_performance
WHERE `services`.`SERVICE_ID` = `service_performance`.`SERVICE_ID`)
where exists (select *
from service_performance
where `services`.`SERVICE_ID` = `service_performance`.`SERVICE_ID`
);
I am assuming that update _services is a typo and should really be update services.
MySQL is complaining because in this query there is no service_performance table
Try it like this:
UPDATE _services s INNER JOIN service_performance p
ON s.SERVICE_ID=p.SERVICE_ID
SET s.MAX_VALUE=MAX(p.AVERAGE_MEMORY) GROUP BY p.SERVICE_ID
I don't have a database to test on, but firstly you haven't given the subquery an alias, so it doesn't know what service_performance.Service_ID is.
Secondly, in this context the subquery needs to return a single value rather than a table, so you may not be able to reference it. If adding the alias to the subquery doesn't work, then something like the following should work:
update _services
set MAX_VALUE = (SELECT MAX(AVERAGE_MEMORY) AS SERVICE_ID FROM service_performance
INNER JOIN
services
on services.SERVICE_ID = service_performance.SERVICE_ID)

Is it possible to delete multiple rows in MySQL if target table is in from clause?

I'm using PHPMYAdmin and MySQL Database. I would like to remove from the table 1masterinventory all the rows (PK RID) that are not in 1amazoninventory (PK RID). I feel like this query should work but I get the error "#1093 - You can't specify target table '1masterinventory' for update in FROM clause."
DELETE FROM 1masterinventory
WHERE rid IN
(
SELECT rid
FROM 1masterinventory
WHERE rid NOT IN
(
SELECT rid FROM
1amazoninventory WHERE 1
)
)
SQL Fiddle:
DELETE m
FROM 1masterinventory m
LEFT JOIN 1amazoninventory a ON a.rid = m.rid
WHERE a.rid IS NULL;

Combine Update and Select Query

I got two MySQL working fine and i'm trying to find a way to combine them into one single query.
First, it selects ID of an employee.
SELECT 'ID' FROM `employee` ORDER BY ID DESC LIMIT 1;
Let's say it returns ID 100;
Then update data of employees whose ID is 100
UPDATE 'LOG' SET `TIME_EXIT`='2013/02/22' WHERE `ID`='100';
Can i do it all in a single query?
Just add them together:
UPDATE LOG SET TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM employee
ORDER BY ID DESC
LIMIT
);
But based on that code currently it'll only ever update the last employee, you will need to select the correct employee by using some other identifier to ensure you have the correct one.
UPDATE LOG SET TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM employee
WHERE NAME = 'JOHN SMITH'
ORDER BY ID DESC
LIMIT 1
);
It's now a few months old, but maybe helps you or others finding this via google…
If you want to UPDATE a field in the same selected table use this:
UPDATE LOG SET
TIME_EXIT = '2013/02/22'
WHERE ID = (
SELECT ID
FROM (
SELECT ID
FROM LOG
WHERE whatEverYouWantToCheck = whateverYouNeed
) AS innerResult
)
So, you SELECT id from a subselect. If you try to subselect it directly, mySQL quites with your error message You can't specify target table 'log' for update in FROM clause, but this way you hide your subsubquery in a subquery and that seems to be fine. Don't forget the AS innerResult to avoid getting the error message #1248 - Every derived table must have its own alias. Also match the subsubquery field name to the subquery field name in case you do something like SELECT COUNT(*) or SELECT CONCAT('#', ID)

MySQL sub query select statement inside Update query

I have 2 tables: tbl_taxclasses, tbl_taxclasses_regions
This is a one to many relationship, where the main record ID is classid.
I have a column inside the first table called regionscount
So, I create a Tax Class, in table 1. Then I add regions/states in table 2, assigning the classid to each region.
I perform a SELECT statement to count the regions with that same classid, and then I perform an UPDATE statement on tbl_taxclasses with that number. I update the regionscount column.
This means I'm writing 2 queries. Which is fine, but I was wondering if there was a way to do a SELECT statement inside the UPDATE statement, like this:
UPDATE `tbl_taxclasses` SET `regionscount` = [SELECT COUNT(regionsid) FROM `tbl_taxclasses_regions` WHERE classid = 1] WHERE classid = 1
I'm reaching here, since I'm not sure how robust MySQL is, but I do have the latest version, as of today. (5.5.15)
You could use a non-correlated subquery to do the work for you:
UPDATE
tbl_taxclasses c
INNER JOIN (
SELECT
COUNT(regionsid) AS n
FROM
tbl_taxclasses_regions
GROUP BY
classid
) r USING(classid)
SET
c.regionscount = r.n
WHERE
c.classid = 1
Turns out I was actually guessing right.
This works:
UPDATE `tbl_taxclasses`
SET `regionscount` = (
SELECT COUNT(regionsid) AS `num`
FROM `tbl_taxclasses_regions`
WHERE classid = 1)
WHERE classid = 1 LIMIT 1
I just needed to replace my brackets [] with parenthesis ().