Update table row with values from same table column row - mysql

I want to update the values of column created_at with values of column updated_at, where value in the column created_at is 0000-00-00.
I tried the following query :
UPDATE tbl_message_types a
SET a.created_at = (
SELECT
c.updated_at
FROM
( SELECT b.updated_at
FROM tbl_message_types b
WHERE b.created_at LIKE '%0000-00-00%'
AND a.id = c.id ) AS c
)
AND a.created_at LIKE '%0000-00-00%';
But the result of the query is as follows :
> 1054 - Unknown column 'a.id' in 'where clause'
> Time: 0.008s
How can I modify the query ?

Your alias is out of place, but I would phrase this update using a join:
UPDATE tbl_message_types a
INNER JOIN UPDATE tbl_message_types b
ON a.id = b.id AND
COALESCE(b.created_at, '0000-00-00') != '0000-00-00'
WHERE COALESCE(a.created_at, '0000-00-00') = '0000-00-00';
Note that it is not clear how you ended up with datetime values of 0000-00-00 in your table. I am treating NULL the same way in my answer, though you may remove the call to COALESCE if you disagree.

I think you can do it in much simplified way.. without any join gimmicks; Why should you insist on a single query..? Do the select First, and update next. Try this..
select
value1 from tableName
where columnX like '%someThing%' and columnY = 'SomeValue' as value11;
update sametableName set someColumn= value11
where....

Related

Using group by in SET clause

I'm trying to update a column of a table so that is equal to the count of something in another table. Like this:
UPDATE TABLE
SET TOTAL = (SELECT COUNT(f1)
FROM TABLE2
GROUP BY f2);
But I keep getting sub query returns more than 1 row, and I can't think of how to fix it.
UPDATE (copied from the comment)
f2 is the relation between TABLE and TABLE2 – Thomasd d
Based on your comment
f2 is the relation between TABLE and TABLE2
you probably want something like this
UPDATE TABLE T1, (SELECT f2, COUNT(F1) cnt FROM TABLE2 GROUP BY f2) T2
SET T1.TOTAL = T2.cnt
WHERE T1.f2=T2.f2
adapt T1.f2 if necessary
UPDATE t1
SET total = ( SELECT COUNT(f1)
FROM t2
WHERE t1.f2 = t2.f2 );
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=91de17deff657f66fa54b42fe20ed3c5
Add WHERE total IS NULL if you do not need to recalculate values for rows which have a value already.
Your subquery is returning multiple values and your SET statement is only expecting one. This might fix your code if that is what you are looking for.
UPDATE TABLE
SET TOTAL = (SELECT COUNT(f1)
FROM TABLE2)

trouble updating column with subselect in mysql

I have dups in my mysql database that I can find with this query:
select checksum, count(*) c from MY_DATA group by checksum having c > 1;
I get a list of about 200 dups. What I want to do is update a column in the same table to mark them as dups.
I tried this:
update MY_DATA SET DONT_PARSE=1
where (select count(*) c from MY_DATA group by checksum having c > 1);
I get the error: You can't specify target table 'MY_DATA' for update in FROM clause
Anyone have a solution for this?
Use join:
update my_data d join
(select checksum, count(*) as cnt
from MY_DATA
group by checksum
having cnt > 1
) c
on d.checksum = c.checksum
set DONT_PARSE = 1 ;
Unfortunately, MySQL doesn't allow you to reference the table being updated in a subquery in the update (or delete). However, join can usually be used to get around this.
your query seems also not correct (seems that the where clause is not complete) so you could use a INNER JOIN on the subquery that retrieve duplicated
You could try using
update MY_DATA m
INNER JOIN (
select checksum, count(*) c
from MY_DATA
group by checksum
having c > 1
) t on t.checksum = m.checksum
SET DONT_PARSE=1

Delete subquery with alias MySQL

I have a problem with this delete query:
DELETE r
FROM table AS r
WHERE r.stoptime IS NULL
and r.address IN
(select address from table where starttime <= r.starttime and stoptime > r.starttime)
I get the following error:
Error : You can't specify target table 'r' for update in FROM clause.
My goal is to delete records that the starttime is contained in another record but I got an error with the alias in the subquery.
Somebody know how to do this? Thanks in advance.
Try to use JOINS like this:
DELETE r
FROM `table` r
JOIN `table` t ON t.id = r.id
WHERE t.starttime <= r.starttime and t.stoptime > r.starttime
AND r.stoptime IS NULL

Intermediate MySQL query: updating table column based on separate table column value

I have a table ticketdetails which contains a column with data that I want to copy to another table's column:
SELECT TicketID, MIN( StartTime ) StartTime
FROM ticketdetails
GROUP BY TicketID #might be unnecessary?
ORDER BY TicketID
The output looks like this:
I want the result of this query, "StartTime", to be supplied in my other table column, tickets.TimeScheduled, where the TicketID matches for both tables.
Right now I'm trying:
UPDATE tickets SET TimeScheduled = (
SELECT MIN( StartTime ) StartTime
FROM ticketdetails
GROUP BY TicketID
ORDER BY TicketID)
WHERE tickets.TicketID = ticketdetails.TicketID
I get this error:
#1054 - Unknown column 'ticketdetails.TicketID' in 'where clause'
I know that this column exists in my database. For some reason the query doesn't recognize the column. I think I need a JOIN or something, but I'm not the best with intermediate-advanced MySQL queries. Help is much appreciated.
Use a join:
UPDATE tickets AS t
JOIN (SELECT TicketID, MIN( StartTime ) StartTime
FROM ticketdetails
GROUP BY TicketID) AS d ON t.TicketID = d.TicketID
SET t.TimeScheduled = d.StartTime
The reason you're getting an Unknown column error is because the ticketdetails table only exists in the subquery, not in the main query.
If you want to set timeScheduled to NULL if there's no matching ticket in ticketdetails, change JOIN to LEFT JOIN.
BTW, there's no need for ORDER BY in the subquery.
You can do this with a correlated subquery:
UPDATE tickets
SET TimeScheduled = (SELECT MIN( td.StartTime ) StartTime
FROM ticketdetails td
WHERE td.TicketID = t.TicketID
);

Confused with UPDATE query. How to convert select to update with MySQL?

I have seen very similar if not same questions on here but my trials of trying to convert following query into an UPDATE statement failed.
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1
ORDER BY t.rank DESC
I want to update column of all results selected using the query above.
How can I convert this into an update statement?
Thank you.
This should do it:
update table
set column = 'somevalue'
where id in
(select id from (
SELECT table.* FROM table JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column) WHERE t.rank = 1) x)
not entirely sure but i think it's something like
update tblname set columname = value where tblname.columncompare = (select statement)
INSERT INTO table (id, value)
SELECT table.id, table.value
FROM table
JOIN (
SELECT column, COUNT(*) AS rank
FROM table
GROUP BY column
) AS t USING (column)
WHERE t.rank = 1
ORDER BY t.rank DESC
ON DUPLICATE KEY UPDATE value = VALUES(value)
Insert on duplicate to the rescue!
Basicly this allows you to do any SELECT as normal and then you prepend INSERT INTO and append ON DUPLICATE.
I guess that this query is made up, but what's the point of filtering and ordering the same column?