I am new to MySQL statements, so bear with me.
I am working with a single table that contains 3 columns.
ID, value, and report ID.
What I am looking to do is update the 'value' to yes of id = 2 WHEN the value is 'yes' for id = 1, and at the same time matching the report id is matching between id 1 and id 2. Apologies if this doesn't make sense, please let me know if I need to clarify better.
Here are some queries I've attempted so far:
UPDATE table
SET value = CASE WHEN value = 'Yes' AND id = 1 AND report_id LIKE report_id
THEN 'Yes' ELSE value END
WHERE (id = 2 or id = 1);
UPDATE table
SET value = 'Yes'
WHERE (report_id = report_id
AND value = 'Yes')
AND id = 1
OR id = 2;
UPDATE table
SET value = IF(value = 'Yes' AND report_id LIKE report_id AND id = 1, 'Yes', '')
WHERE id = 2;
Example Table:
id
value
report_id
1
yes
1001
1
no
1002
1
yes
1003
2
1001
2
1002
3
cat
1001
5
1002
All your conditions are being processed on a single row, not comparing the id=1 rows to the id=2 rows with the same report_id.
You can use a self-join to compare different rows of the table with a relationship.
You don't need CASE, you can select the rows to update using the WHERE conditions.
UPDATE t1
JOIN t2 ON t1.report_id = t2.report_id
SET t1.value = 'yes'
WHERE t1.id = 2 AND t2.id = 1 AND t2.value = 'yes'
If I understood correctly, please try to use something like below to get the result. You need to use the same table in exists condition as well. I hope my answer helps you.
update table t set t.value = 'Yes' where exists ( select 1 from table t1 where
t1.value = 'Yes' and t1.id = 1 and t.report_id = t1.report_id) and t.id = 2;
UPDATE
table a
JOIN table b ON a.report_id = b.report_id
SET
a.value = 'Yes'
WHERE
a.id = 2
AND b.id = 1
AND b.value = 'Yes';
Related
id|code|transaction_date |amount|status|record_status|creation_date
1 |0001|2021-12-10 00:00:00| 10.00|Succ |D |2021-12-10 00:00:00
2 |0001|2021-12-10 00:00:00| 10.00|Fail |D |2021-12-11 00:00:00
3 |0001|2021-12-10 00:00:00| 10.00| |A |2021-12-12 00:00:00
I want to select recod_status = A, code = 001 and transaction date is 2021-12-10'
SELECT * FROM TestTable
WHERE DATE(alloc.trans_date) IN ('2021-12-10') // Need filter by few dates
AND record_status = 'A'
AND code = '001';
but I only want the record to return if the same code = 001 and transaction date is 2021-12-10' but record_status = 'D', status = 'Fail' and the creation_date is latest.
Example above will return 3rd record because 2nd record which creation_date is the latest and status = 'Fail' and record_status = 'D'.
I just want to compare the latest creation_date.'
I can select the latest transaction where the record_status = 'D'
SELECT * FROM TestTable
WHERE creation_date = (
SELECT MAX(creation_date)
FROM TestTable
WHERE DATE(trans_date) IN ('2021-12-10')
AND record_status = 'D'
)
AND record_status = 'D'
AND code = '001';
Looks like:
SELECT t1.*
-- or SELECT t2.* - depends on the task which is unclear for me yet
FROM ( SELECT *
FROM table
WHERE DATE(trans_date) = '2021-12-10'
ORDER BY creation_date DESC LIMIT 1 ) t1
JOIN table t2 ON DATE(t1.trans_date) = DATE(t2.trans_date)
AND t1.creation_date > t2.creation_date
WHERE t1.record_status = 'A'
AND t2.record_status = 'D'
AND NOT EXISTS ( SELECT NULL
FROM table t3
WHERE DATE(t1.trans_date) = DATE(t3.trans_date)
AND t1.creation_date > t3.creation_date
AND t3.creation_date > t2.creation_date )
Subquery t1 selects the most last row, and further it is checked for status A.
Table copy t2 selects the row which is adjacent to the row selected in subquery (NOT EXISTS checks that there is no any row between), and further it is checked for status D.
I have the following data:
-------table_a-------
| id | data | value |
| 1 | 5 | 1 |
| 2 | 3 | 3 |
My desired output is to merge row 2 with row 1 so that where id = 1, data remains to =5 but the values are added, so 1+3.
-------table_a-------
| id | data | value |
| 1 | 5 | 4 |
This is as far as I got with the queries but the first isn't seeming to work. Also this is not for retrieving the data, this is for manipulating the data in the database.
Current queries (1st not working):
UPDATE table_a SET value = value + (SELECT a.value FROM table_a a WHERE a.id = 2) WHERE id = 1;
DELETE FROM table_a WHERE id = 2;
If this is the logic that you want:
UPDATE table_a
SET value = value + (SELECT a.value FROM table_a a WHERE a.id = 2)
WHERE id = 1;
DELETE FROM table_a WHERE id = 2;
The update is not going to work because you cannot reference the table being updated in the rest of the query. You can use a hack for the update and do:
UPDATE table_a
SET value = value + (SELECT value FROM (SELECT a.value FROM table_a a WHERE a.id = 2) )
WHERE id = 1;
The double select fixes this problem. The more correct method is to use join:
UPDATE table_a a CROSS JOIN
(SELECT a.value FROM table_a a WHERE a.id = 2) as a2
SET a.value = a.value + a2.value
WHERE id = 1;
Here is one way on how you can make an accumulative column by joining the table with itself.
I'm assuming the Id column is an identity column,
Hope this helps.
CREATE TABLE [dbo].[TestTable](
[id] [int] NULL,
[data] [int] NULL,
[value] [int] NULL
) ON [PRIMARY]
INSERT INTO [dbo].[TestTable]
([id]
,[data]
,[value])
VALUES
(1,5,1),
(2,3,3),
(3,4,4)
SELECT a.id , a.data , a.value + b.value as Sum
FROM [TestTable] a
left join [TestTable] b
on a.id + 1 = b.id
This will work, please check.
UPDATE table_a,
(SELECT value as val FROM table_a where id = 2) AS t2
SET table_a.value = table_a.value+t2.val
where table_a.id = 1;
DELETE from table_a where id = 2;
Here is my super-simple table layout...
id | order
1 | 1
2 | 2
I've been trying to update the order for both entries with a single query however my query tests seem to keep updating the auto_increment id field.
My goal is to make id1 = order 2 and id2 = order1 in a single query. What am I doing wrong with my query?
UPDATE forms
SET order = CASE id
WHEN 1 THEN 2
WHEN 2 THEN 1
END
WHERE id IN (1,2);
How about doing JOIN?
UPDATE Tablename AS a
INNER JOIN Tablename AS b
ON a.id = 1 AND b.id = 2
SET a.order = b.order,
b.order = a.order
SQLFiddle Demo
I have a table named status_t:
**id** **page** **status** **locked**_by
1 0 2 0
1 1 2 0
1 2 2 0
2 0 0 0
2 1 2 0
2 2 2 0
primary key is ( id, page) .
In the above example I would like to update all the
rows that all pages have status = 2.
i.e. the query should update all the rows with id = 1 to status 3. So the table will become
**id** **page** **status** **locked**_by
1 0 3 1
1 1 3 1
1 2 3 1
2 0 0 0
2 1 2 0
2 2 2 0
I have tried:
SELECT * FROM status_t AS t
WHERE id IN
(SELECT id FROM status WHERE status = 0) LIMIT 10
the above query fetches the rows to be updated but I cannot do that:
UPDATE status_t as S1 WHERE id IN
(SELECT id FROM status_t WHERE status = 2)
SET S1.status = 3, S1.locked_by = 1
EDIT:
THE ABOVE TABLE IS JUST AN EXAMPLE.
I do not want to update WHERE id = 1 . I just want to update rows no matter the id
that have status = 2 for the same id.
In the above example if the row with key (2, 2) had status = 2 then it should be updated.
This should work:
update status_t t
join (
select distinct id from status_t s
where status=2 and not exists (
select 1 from status_t ss
where s.id=ss.id and s.status <> ss.status
)
) i on i.id = t.id
set t.status = 3
The inner query selects IDs where all values of status are set to 2. It achieves this result by checking that tere are no rows with the same ID and a different status.
Here is a demo on sqlfiddle.
Try this:
UPDATE status_t a
INNER JOIN (SELECT id FROM status_t WHERE STATUS = 2 GROUP BY id HAVING COUNT(*) = 3 LIMIT 1) AS b ON a.id = b.id
SET a.status = 3, a.locked_by = 1;
This will update data as you want for all pages have status = 2
The following should do the trick:
UPDATE status_t
SET status = 3, locked_by = 1
WHERE status = 2
There should be no need to use a sub-query, as the WHERE should be able to be used directly in the UPDATE statement.
EDIT:
I just noticed that you only had an update against the rows with ID = 1, which does not match the statement 'In the above example I would like to update all the rows that all pages have status = 2.. The rows whereid = 2, andstatus = 2` would also be updated by my query.
If you need to only update a particular id, add the following at the end of my query:
AND id = 1
try this
update status_t set staus=3 ,locked_by=1
where id=1
or
update status_t set status=3 ,locked_by=1
where status=2
UPDATE status_t SET status = 3, S1.locked_by = 1 WHERE id = 1
Try this::
UPDATE status_t SET status = 3, locked_by = 1 WHERE status = 2
And if you want to achieve this in your way :
UPDATE
status_t as S1
INNER JOIN
(
SELECT keyword_id
FROM status_t WHERE status = 2
) as tempStatus ON id= tempStatus.keyword_id
SET S1.status = 3, S1.locked_by = 1
I am trying to transfer some data between tables. The 'NEW' table can have multiple entries of the data that was originally not meant to have multiple entries in the 'OLD' table. I would like to take the data from the 'OLD' table and copy it over to the new table where the NEW.ID is the lowest where new.OtherID=old.OtherID, basically a MIN(ID) per group of OtherID's equal to each other.
'NEW' table
ID | OtherID | Data
1 1 NULL
2 1 NULL
3 2 NULL
4 3 NULL
5 3 NULL
'OLD'
OtherID | Data <br>
1 data1
2 data2
3 data3
4 data4
5 data5
Desired Outcome on updated 'NEW' table:
ID | OtherID | Data <br>
1 1 data1
2 1 NULL
3 2 data2
4 3 data3
5 3 NULL
etc
Thanks!
This is how you could use INNER JOIN with UPDATE in MySQL:
UPDATE NEW n
INNER JOIN (
SELECT
OtherID,
MIN(ID) AS ID
FROM NEW
GROUP BY OtherID
) m ON n.ID = m.ID
INNER JOIN OLD o ON n.OtherID = o.OtherID
SET n.Data = o.Data
You can try:
UPDATE new
SET Data = ( SELECT DATA FROM old WHERE otherID = new.otherID )
WHERE NOT EXIST
( SELECT NULL FROM new AS new2
WHERE new2.id < new.id
AND new2.otherID = new.otherID )
Note that this is standard SQL92 and should work with any RDBMS.
This worked for me in PostgreSQL, though I may have gotten the quoting wrong for MySQL.
UPDATE newtable SET
`Data` = oldtable.`Data`
FROM
oldtable
WHERE
newtable.`ID` IN (
SELECT MIN(sub_newtable.`ID`)
FROM newtable sub_newtable
GROUP BY
sub_newtable.`OtherID`
)
AND newtable.`OtherID` = oldtable.`OtherID`
You can use:
UPDATE `NEW`
LEFT JOIN `OLD`
ON `NEW`.`OtherID` = `OLD`.`ID`
SET `NEW`.`Data` = `OLD`.`Data`
EDIT: I'm sorry, this will update all records that correspond to columns in OLD.