I want to combine three different UPDATE queries, in different table, with different WHERE conditions, into a single mySql query. Is it possible?
Reason: send 1 request to mySql server is faster than sending 3
requests separately :)
UPDATE client SET clientCount = clientCount + 1 WHERE clientType = 2
UPDATE storage SET soldItem = soldItem + 1 WHERE itemType = 5
UPDATE employee SET doWork = 1, totalSale = totalSale + 1 WHERE employeeId = 12
The UPDATE statements are independent, and are not related to each other. I tried to find some solution, however, the
UPDATE client, storage, employee SET client.clientCount = ... , storage.soldItem = ... WHERE ... ? ? ? ...
does not fit to my case as my three UPDATE statements are independent...
Is it possible to combine 3 independents queries into a 1 query?
Create a stored procedure with all these update statements and call the stored procedure from your code.
Related
I´m trying to insert into a table but it gives me the same error, and I dont understand why, because as I know, I can insert into a table multiple rows as long as the name columns match.
INSERT INTO top_semanal (id_usu, deporte)
SELECT DISTINCT actividades.dueno, actividades.deporte
FROM actividades
INNER JOIN usuario
ON actividades.dueno=usuario.id
WHERE actividades.deporte='Escalada'
ORDER BY usuario.Exp DESC
LIMIT 3;
This is the query. And as aditional info, it is very strange because with the same data in my database, sometimes work and sometimes doesn´t. And I need this query to be a trigger so I need this working the 100% of the times.
The goal here is get the 3 of the users with the most experience in the app, filtered by the sports they practice, and insert them into a "weekly top". I have a table "activities" where the users can add their activities, a table users and a table top3. The activities and user is related 1:M
¿Any help, I´m using xampp with mysql?
Edit: there is a trigger when the table is inserted:
BEGIN
UPDATE insignias_usuarios
SET proceso_Top = proceso_Top + 1
WHERE id_usu = NEW.id_usu;
IF ((SELECT proceso_Top FROM insignias_usuarios WHERE id_usu=NEW.id_usu AND proceso_Top=1 OR
proceso_Top=3)IS NOT NULL) THEN
UPDATE insignias_usuarios
SET nivel_Top = nivel_Top + 1, nivel_Top_Next = nivel_Top_Next + 1
WHERE id_usu = NEW.id_usu;
ELSEIF((SELECT proceso_Top FROM insignias_usuarios WHERE id_usu=NEW.id_usu AND proceso_Top=10)IS NOT NULL) THEN
UPDATE insignias_usuarios
SET nivel_Top = nivel_Top + 1, nivel_Top_Next = 1
WHERE id_usu = NEW.id_usu;
END IF;
END
¿May the error be because the trigger is called at the same time instead of 1 by 1 for each insert?
I have the following SQL, which gives me the error that this union tabled called brokeredTable is not updateable.
UPDATE (SELECT chid,brokered,bid,uid,rate FROM spot_channels UNION SELECT tid,brokered,bid,uid,rate FROM tremor_tags) as brokeredTable SET brokered = 1, rate = 5, bid = 5, uid = 7 WHERE chid = 110399
As you can see the SQL is pretty simple, instead of running two update statements on two different tables I wanted to Union them into one set and then run the update against that set of data. Which apparently I cannot do this way.
Any Suggestions? Again I just want one SQL statement to accomplish this.
"The SQL UNION operator combines the result of two or more SELECT statements"
That query doesn't even have the UPDATE sintax. UPDATE table SET column = 'value'.
You should search before ask:
Performing an UPDATE with Union in SQL
In MySQL, how can I use use union with two update statements, or run two update statements simultaneously, so I get the count of affected rows of both (together)?
I'm trying to run the two of them together, instead of having two separate ones. The two of them update two different rows, so they cannot be run together in a single statement. Any help?
Example:
update prodb set buyer = buyer+1 where userId = 1
union
update prodb set seller = seller+1 where userId = 2;
Update: Did it this way, but it still returns affected rows as one. I guess it's returning the number of rows from the second statement only.
$stmt = '
update prodb set buyer = buyer+1 where userId = 1;
update prodb set seller = seller+1 where userId = 2;
';
$update = $dbc->prepare($stmt);
$update->execute();
Don't use union for update. Use multi_query (in case you are running php).
$db_link->multi_query("update prodb set buyer = buyer+1 where userId = 1; update prodb set seller = seller+1 where userId = 2;");
Never forget to add semicolon at last sql statement.
i have a registry table ( id , counter , group_name , type )
and group tables which includes these tables
software_group
website_group
news_group
etc ..
every group is in the registry table as well as it's own group table
now if i want to update a row in the registry i want appropriate group table to updated too
type column indicates the group table so i have it something like :
UPDATE registry JOIN
CASE WHEN registry.type = 1 THEN software_group
CASE WHEN registry.type = 2 THEN website_group
CASE WHEN registry.type = 3 THEN news_group
AS other_table
ON registry.id = other_table.reg_id
SET registry.name = $newname
,
other_table.name = $newname
WHERE registry.id = $id
is it possible to do something like this ? i can just select the registry row and do the job with php but i thought join/update would be faster than select/update
I think the dynamic join to tables on run time will be best if you handle the conditions in application layer, rather than the DB layer
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 ().