Inner Sql for Update Not working Mysql 5.6 - mysql

I have a table with following columns
CommentId
ParentCommentId
ChildCommentCount
Update PostComment t1
set ChildCommentCount = (select count(*) from PostComment t2 where t2.CommentId = t1.ParentCommentId);
Trying to update ChildCommentCount with total count of where current CommentId shows up as a parent
but this sql above gvies me error like
Error Code: 1093. You can't specify target table 't1' for update in
FROM clause
any ideas how to rewrite this sql?

You can't select same table while you are updating it this can be done by using join
Update PostComment t1
join (
select CommentId ,count(*) total from PostComment group by CommentId
) t2 on(t2.CommentId = t1.ParentCommentId)
set t1.ChildCommentCount = t2.total

Related

MySQL Query to rename duplicate column

Thanks to this question Rename Mysql Duplicate Value I was able to come up with this queryn to elminate the duplicate rows.
UPDATE table1
inner join (SELECT OBJECTID,CONCAT(IDENT,'_1') as IDENT FROM table1
GROUP BY IDENT HAVING COUNT(*) > 1) t
on t.OBJECTID = table1.OBJECTID
SET table1.IDENT = t.IDENT;
This works well but I want to only rename the rows where the column IDENT is duplicated and the NAME column is different. Any ideas how to do this?
Change the grouping to be both NAME and IDENT.
UPDATE table1
JOIN (
SELECT MAX(objectid) AS max_id, name, CONCAT(ident, '_1') AS new_ident
FROM table1
GROUP BY name, ident
HAVING COUNT(*) > 1
) AS t ON t.max_id = table1.objectid
SET table1.ident = t.new_ident

MySql select and update and return the updated ids (in atomic way)

in MySql 8 i need to execute some sql statement in atomic way.
1)I have to select rows that matches the condition (limit to 10)
SELECT Id from Table1 where Reserved is false LIMIT 10
2)I need to update the selected rows setting the column Reserved to True:
UPDATE Table1 SET Reserved = true Where ....
I have to return the updated rows ids to client.
How can i do?
Thanks
assuming your table1 is not the same as AdsProfile and the table1 table are not influenced by uthe update for AdsProfile
You could try
using an upodate wuith join on a subquery
UPDATE AdsProfile a
inner join (
SELECT Id from Table1 where Reserved is false LIMIT 10
) t on t.id = a.id
SET a.Reserved = true
and for the updated row should be
select id from table1 t1
inner join (
SELECT Id
from Table1 where Reserved is false LIMIT 10
) t2 on t2 id= t1.id
inner join (
select id from AdsProfile a
inner join (
SELECT Id from Table1 where Reserved is false LIMIT 10
) t on t.id = a.id
where a.revered = true
) t3.t3.id = t1.id
CREATE PROCEDURE sp_name ()
BEGIN
DROP TEMPORARY TABLE IF EXISTS tmp_table_name;
CREATE TEMPORARY TABLE tmp_table_name
SELECT Id FROM Table1 WHERE Reserved IS FALSE LIMIT 10;
UPDATE AdsProfile JOIN tmp_table_name ON ...
SET Reserved = true
Where ....;
SELECT * FROM tmp_table_name;
DROP TEMPORARY TABLE tmp_table_name;
END
On the client side execute the next SQL statement:
CALL sp_name;

MySQL: delete rows with "WHERE ... NOT IN" from only one single table

In a MySQL database I have a many-to-many relationship between two tables. For the sake of simplicity let's assume those tables map homes and their residents. I have a third table to map those relations (home_resident_relations). The latter table has an additional column datemodified that stores the date of the latest update of each row via triggers.
Now I want to get rid of all former residents for each home and only keep the current ones - that is those with the newest date.
I have already a working SELECT clause that will list me all old relations I want to delete:
SELECT * FROM `home_resident_relations` WHERE `resident_id` NOT IN
(SELECT tbl.`resident_id`
FROM `home_resident_relations` tbl
WHERE tbl.`datemodified` =
(SELECT max(tbl2.`datemodified`)
FROM `home_resident_relations` tbl2
WHERE tbl2.`home` = tbl.`home`
GROUP BY tbl2.`home`)
OR tbl.`datemodified` IS NULL
);
Now it would be a straight-forward idea to simply replace the SELECT * with a DELETE command to remove all those rows. However, this does not work due to error
#1093 - You can't specify target table 'home_resident_relations' for update in FROM clause
So here's my question:
How do I delete from a table while using it in the WHERE ... NOT IN clause?
Use a left join instead:
DELETE hrr
FROM `home_resident_relations` hrr LEFT JOIN
(SELECT tbl.`resident_id`
FROM `home_resident_relations` tbl
WHERE tbl.`datemodified` = (SELECT max(tbl2.`datemodified`)
FROM `home_resident_relations` tbl2
WHERE tbl2.`home` = tbl.`home`
GROUP BY tbl2.`home`
) OR
tbl.`datemodified` IS NULL
) tt
ON hrd.resident_id = tt.resident_id
WHERE tt.resident_id IS NULL;
This works for both the SELECT and DELETE.
Try using DELETE with join:
DELETE FROM `home_resident_relations`
LEFT OUTER JOIN
(SELECT tbl.`resident_id`
FROM `home_resident_relations` tbl
WHERE tbl.`datemodified` =
(SELECT max(tbl2.`datemodified`)
FROM `home_resident_relations` tbl2
WHERE tbl2.`home` = tbl.`home` )
OR tbl.`datemodified` IS NULL) s
ON(s.`resident_id` = `home_resident_relations`.`resident_id`)
WHERE s.`resident_id` is null

Mysql update column with value from another table. Error: "count doesn't match"

i have this situation:
TABLE ASSET
TABLE USERS
and now i wanna be delete name \surname columns on device table and fill id_users with relative id_users of users table.
Sample:
I've tried with these queries:
1)
UPDATE asset t1
INNER JOIN users t2
ON t1.name = t2.name and t1.surname = t2.surname
SET t1.id_users = t2.id_users
2)
UPDATE asset
SET asset.id_users = (
SELECT users.id_users
FROM users
WHERE asset.name = users.name AND asset.surname = users.surname
);
Both queries I get this error :
#1136 - Column count doesn't match value count at row 1
Can you help me??

DELETE FROM table WHERE NOT MAX

Okay so I have a table that has xid. Each xid can have several pids. I am trying to delete everything except the row that has the highest pid for each xid.
I am trying:
DELETE FROM table WHERE `pid` NOT IN
( SELECT MAX(`pid`)
FROM table
GROUP BY `xid`
)
If I use the same query but with SELECT instead of DELETE, I get all of the records that I want to delete. When the DELETE is there, I get the error:
#1093 - You can't specify target table 'mod_personnel' for update in FROM clause
Use a JOIN rather than NOT IN:
DELETE t1.* FROM table t1
LEFT JOIN (SELECT xid, MAX(pid) pid
FROM table
GROUP BY xid) t2
ON t1.pid = t2.pid
WHERE t2.pid IS NULL
DELETE FROM table WHERE `pid` NOT IN
(SELECT maxpid FROM
( SELECT MAX(`pid`) as maxpid
FROM table
GROUP BY `xid`
)as m
)