SELECT UPDATE ALL ROWS AT THE SAME TIME MYSQL - mysql

I'm trying to update and select the same table as the following:
UPDATE tbl_domain SET remain =
(SELECT DATEDIFF(expdate,NOW()) FROM (
SELECT DATEDIFF(expdate,NOW()) FROM tbl_domain
) as x )
How can i make it work ? Someone please help me!!!

Try this:
UPDATE tbl_domain t, (SELECT DATEDIFF(expdate,NOW()) as remain
FROM tbl_domain
) t1
SET t.remain = t1.remain
-- WHERE t.Id = t1.Id --If you have some kind of unique id in your table.

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)

How can I do own increment value in mysql

I'm struggling to do proper sql script to increment field on specific way.
Those two script are without any exception, but nothing happened on the results.
Script 1:
UPDATE
myTable T1,
(
SELECT id,
(#s:=#s+1) AS seq
FROM myTable, (SELECT (#s:=0) AS s ) s
WHERE infotext IS NULL ORDER BY grouptext
) T2
SET sequence = seq
WHERE T1.id = T2.id
Script 2:
UPDATE myTable AS target
INNER JOIN (
SELECT supfault_id,
(#s:=#s+1) AS seq
FROM myTable, (SELECT (#s:=0) AS s ) s
WHERE infotext IS NULL ORDER BY grouptext
) AS ordered ON ordered.id = target.id
SET sequence = seq
This one get the last desc value from table1 and increment by one then update the table2:
set #inc = 0;
select cast(valToIncrement as signed) into #inc from
(select REPLACE(fkid,' ','') as valToIncrement from tbl_1 ORDER BY fkid)as a ORDER BY valToIncrement desc limit 1;
update tbl_2 set fkid = #inc + 1 where fkid = 122;
Subqueries working well separately, so I wondered why I can't update my sequence value by seq from subquery.
I'm not expert, but I felt that need to be used some virtual table for my subquery.
Here is solution for inner join case:
CREATE TEMPORARY TABLE supportGroupSeqcalculation AS
SELECT supfault_id,
(#s:=#s+1) AS seq
FROM myTable, (SELECT (#s:=0) AS s ) s
WHERE infotext IS NULL
ORDER BY grouptext;
UPDATE myTable AS target
INNER JOIN supportGroupSeqcalculation AS ordered ON ordered.supfault_id = target.supfault_id
SET sequence = seq;
DROP TEMPORARY TABLE supportGroupSeqcalculation;
We can get into temporary table specific order and record it as sequence value.
It is not necessarily to drop temporary table, it exists only in current session.

SQL: update a record if a certain kind of a record is not in the same table

I want to update a certain record of a table only if certain another record does not already exist in the table.
I tried a SQL similar to following.
update mytable
set val = 'someval'
where id = 'someid' and
0 = (select count(*) from mytable where col='val2');
This fails with following error.
You can't specify target table 'mytable' for update in FROM clause
Only one process is updating this table, so preserving the atomicity of the operation is not necessary.
I know I can do this using two SQL queries, but is there a way to do this in a single query?
Because you are referring to the same table, the best way to do this uses LEFT JOIN:
update mytable t left join
mytable t2
on t2.col = 'val2'
set val = 'someval'
where t.id = 'someid' and t2.col is null;
There are several ways to do this. Here's one option using a subquery with not exists:
update mytable
set val = 'someval'
where id = 'someid'
and not exists (
select 1
from (select * from mytable) t
where col = 'val2')
SQL Fiddle Demo
Using the subquery bypasses the error you are receiving. Other approaches include outer join with null checks or using not in -- depends a bit on the data.
TRY this:
UPDATE mytable SET val = 'someval'
WHERE id = 'someid' AND col <> 'val2'

Can I combine 2 tables in MySQL and update a field?

Ok so what I need to do here is, take the sum of all the data with a specific name in the first table.
SELECT sum(DKP_Change) FROM 'Attendance' WHERE Name='harrian'
then in a DIFFERENT table I need to update the Total_DKP with the sum of the previous table
SELECT Total_DKP FROM `Characters` WHERE Name='harrian'
I tried the following solution and a few others but I'm not getting any working results
SELECT Total_DKP FROM `Characters` WHERE Name='harrian'
set Total_DKP = (SELECT sum(DKP_Change) FROM 'Attendance' WHERE Name='harrian')
To change data in a row, use the UPDATE statement:
UPDATE Characters AS c
SET c.Total_DKP = ( SELECT SUM(a.DKP_Change)
FROM Attendance AS a
WHERE a.Name = 'harrian'
)
WHERE c.Name = 'harrian' ;
UPDATE `Characters`
SET Total_DKP = (SELECT SUM(DKP_Change) FROM `Attendance` WHERE Name='harrian')
Try the following please.
update t1 set t1.secondcolumn =
(SELECT sum(blah) as blahsum
FROM t1 b
where b.name = 'harran'
)
WHERE t1.name = 'harran'
;

How to update a table using a select group by in a second one as the data source in MySQL?

I can't do this in MySQL
UPDATE tableA, tableB
SET tableA.column1 = SUM(tableB.column2)
WHERE tableA.column3 = tableB.column4
GROUP BY tableB.column4
;
Neither can I
UPDATE tableA,
(
SELECT SUM(tableB.column2) sumB, tableB.column4
FROM tableB
GROUP BY tableB.column4
) t1
SET tableA.column1 = sumB
WHERE tableA.column3 = column4
;
Besides it being illegal code, I think you can understand what I tried to do with the queries above. Both of them had the same intent.
How can I do that in MySQL?
This would be one way, if you don't mind using a subquery:
UPDATE tableA
SET column1 = (
SELECT sum(column2)
FROM tableB
WHERE tableA.coumn3 = tableB.column4);