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

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

Related

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.

Error "operand should contain 1 column(s)" when using SET to calculate percentage in SQL

I created table productivityByUser with columns user and avgEncHr (and more that aren't relevant). I am trying to calculate the percent of each avgEncHr and want to call that PercAvgEncHr. I only want to show all columns where PercAvgEncHr > 25.
I've tried a lot of different things like altering the table and column, etc. and it gives me error: Operand should contain 1 column(s).
SET #count = (SELECT COUNT(user)
FROM productivityByUser);
SET #totalAvgEncHr = (SELECT SUM(avgEncHr)
FROM productivityByUser);
-- ALTER TABLE productivityByUser
-- ADD COLUMN PercAvgEncHr INTEGER;
SET #PercAvgEncHr = (
SELECT avgEncHr, #totalAvgEncHr,(avgEncHr) / ((SUM(avgEncHr)) * 100)
FROM productivityByUser);
SELECT * FROM productivityByUser WHERE (PercAvgEncHr > 25);
SET #PercAvgEncHr = ( SELECT avgEncHr/ SUM(avgEncHr) * 100 FROM productivityByUser );
Now you store only one record. Hope it helps!

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

MySQL: handle EMPTY SET in UPDATE statement

I have: something like
UPDATE table
SET field = (SELECT field FROM another_table WHERE id = #id);
Problem: SELECT field FROM another_table WHERE id = #id subquery can return one field or EMPTY SET.
Question: How to handle situation when subquery returns empty set?
Updated:
UPDATE table t
SET field = IF((SELECT field FROM another_table WHERE id = #id) IS NOT NULL, -- select field
(SELECT field FROM another_table WHERE id = #id), -- Problem #1: select field AGAIN!
(SELECT field FROM table WHERE id = t.id) -- Problem #2: try to not change value, so select the current field value!!
);
If function can be useful:
UPDATE table
SET field = if((SELECT field FROM another_table WHERE id = #id) IS NULL,true,false);
You can add the conditional:
WHERE (SELECT COUNT(*) FROM another_table WHERE id = #id) > 0
This will make sure that at least one row exists in another_table with the id. See my SQL Fiddle as an example.
Note: this may not be the most efficient because it does a count on another_table, and if it is greater than 1 it will do another SELECT (two sub-queries). Instead, you can do an INNER JOIN:
UPDATE table
INNER JOIN another_table ON table.id=another_table.id
SET table.field = another_table.field
WHERE another_table.id = #id;
See this SQL Fiddle. The reason why I saved this as a second option, is not all SQL languages can UPDATE with joins (MySQL can). Also, you need some way to relate the tables..in this case I said that the table.id we are updating is equal to another_table.id we are taking the data from.
NOTE The UPDATE statement will modify EVERY row in table and assign the same value to every row; that seems a little unusual.
To answer your question:
If you want to handle the "empty set" by not updating any rows in table, then one way to do this is with a JOIN to an inline view:
UPDATE table t
CROSS
JOIN (SELECT a.field
FROM another_table a
WHERE a.id = #id
LIMIT 1
) s
SET t.field = s.field
Note that if the inline view query (aliased as s) return an "empty set", then no rows in table will be updated, because the JOIN operation will also return an "empty set", meaning there are zero rows to be updated.

SQL query error - Selecting from table with conditions

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