update with select and case statement in mysql - mysql

can anyone help me with this query what is going wrong in this query.
Query
update vcd_deals
set del_date='2015-09-17'
where case
when (
(select count(del_id)
from vcd_deals
where del_date=curdate()+1
and del_superSaver_deal=1)=0) then 1
else 0
end
order by del_date asc limit 3
I am getting the following error:
Error
You can't specify target table 'vcd_deals' for update in FROM clause.
Actually I want to update first three rows of vcd_deals table only when
(select count(del_id) from vcd_deals where del_date=curdate()+1 and del_superSaver_deal=1)=0.
please suggest is there any other way to do this.
Thanks

That is the problem with MySQL Parser. You can try Inner Join instead.
Otherwise you can enclose your subselect into a separate table like below:(not tested)
update vcd_deals
set del_date='2015-09-17'
where 0 = ( select Del_id_count
from (select count(del_id) as Del_id_count
from vcd_deals
where del_date=curdate()+1
and del_superSaver_deal=1) temp
)
order by del_date asc limit 3

You can try as per below-
UPDATE vcd_deals
SET del_date='2015-09-17'
WHERE del_date<>ADDDATE(CURDATE(), INTERVAL 1 DAY)
AND del_superSaver_deal<>1
ORDER BY del_date LIMIT 3;

Related

Error occur when try to delete via select #1241 - Operand should contain 1 column(s)

I`m trying to delete a lot of data via select. This select work appropriate and returns in result 75k+ rows. I need to delete them, but when I try to delete it this error occurs
#1241 - Operand should contain 1 column(s). I'm using PHPMyAdmin.
DELETE FROM `crm_wsal_metadata`
WHERE `occurrence_id` = ANY
(SELECT *
FROM `crm_wsal_metadata`
WHERE `name` = `PostDate` AND `value` BETWEEN str_to_date('2018-12-26', '%Y-%m-%d') AND str_to_date('2020-05-31', '%Y-%m-%d')
GROUP BY `occurrence_id`)
Use
... SELECT `occurence_id` ...
instead of SELECT *. The group by clause forces you to use only grouped columns and aggregations, not star (perhaps unless some proprietary quirks I don't recommend to rely on).
I had found the answer and will try to write it step by step:
Why does this error happen?
In MySQL, you can't modify the same table which you use in the SELECT part.
This behavior is documented at http://dev.mysql.com/doc/refman/5.6/en/update.html
How to make such thing happen?
There are two ways:
Join the table to itself
UPDATE tbl AS a
INNER JOIN tbl AS b ON ....
SET a.col = b.col
Nest the subquery deeper into a from clause
UPDATE tbl SET col = (
SELECT ... FROM (SELECT.... FROM) AS x);
Personally, in my case the code looked like this:
DELETE FROM crm_wsal_metadata
WHERE occurrence_id = ANY (
SELECT occurrence_id FROM (
SELECT occurrence_id FROM crm_wsal_metadata WHERE name = "PostDate" AND value BETWEEN str_to_date('2018-12-26', '%Y-%m-%d') AND str_to_date('2020-05-31', '%Y-%m-%d') AS search) )
Sorry for such bad styling. Im new with it :)

In DB table, How to update query after select query?

I want update query after select query.
first SELECT query , SELECT * from be_settings order by seq desc limit 1
this select query is based on seq, retrieve the last inserted record.
I want update on this record.
second Update Query, UPDATE be_settings set appgubun ='CCTV', running ='on'.
how to update query after select query?
thanks.
You can do this via an update with a subquery:
UPDATE be_settings
SET appgubun = 'CCTV', running = 'on'
WHERE seq = (SELECT t.max_seq FROM (SELECT MAX(seq) AS max_seq FROM be_settings) t );
The subquery in the WHERE clause is necessary because it involves the be_settings table, which is the target of the update. The following would give an error:
WHERE seq = (SELECT MAX(seq) FROM be_settings)
You need to do
UPDATE be_settings set appgubun ='CCTV', running ='on' where seq= '4'
Assuming your there is seq column on be_settings and it is primary key.
and 4 is the last inserted id you are getting from select query.
if you are getting that seq in any variable based on programming language then you have to use that particular variable.
You have to this if you do not want to use subquery.

MySQL Trigger - Update last row from table with average taken from another table

I'm having problems with creating MySQL trigger - I want to update column temp of last row of table avg_temp with an average from the last 144 records from
the temperature_C column in stats table. I am doing this through phpmyadmin before INSERT happens.
My code, hope it helps to explain what I want the code to do:
UPDATE avg_temp(`temp`)
SET (
SELECT `id`, AVG(`temperature_C`)
FROM `stats`
GROUP by `id`
LIMIT 144
)
ORDER BY id DESC
LIMIT 1
this however throws a syntax error.
If anyone could help me then that would be wonderful.
Yyou could use some subselect
and for the right avg result you should use a subselect for get 144 rows
update avg_temp
set temp = ( select avg(t1.temperature_C)
from (
SELECT id, temperature_C
FROM stats
ORDER BY id
LIMIT 144
) t1
)
where id = your_id
This should be the right syntax for MySQL update to calculate the average of last 144 values, using a sublquery:
UPDATE avg_temp SET `temp` = AVG(
(
SELECT `temperature_C`
ORDER BY id DESC
LIMIT 144
)
)
ORDER BY id DESC
LIMIT 1
You appear to want:
UPDATE avg_temp
SET `temp` = (SELECT AVG(temperature_C)
FROM (SELECT s.temperature_C
FROM stats s
ORDER BY id DESC
LIMIT 144
) s
)
ORDER BY id DESC
LIMIT 1;
However, I would be very suspicious about your desire to do this, especially in a trigger. Normally, the effect that a trigger has depends on the data in the records passed to the trigger. I cannot think of a situation where I have ever written a trigger (other than for testing or informational purposes) that does not reference such data.
You appear to be using the insert as a timing mechanism. Instead, you might just want to create a view that returns the average from the last 144 rows (or from the last 144 rows but one).

Mysql Update Table with Multiple Sub-Queries within same table

I am converting a column datetime format within the same table using multiple sub-queries. I am getting an error that my table does not exist.
Here is my query:
update mytable as t
set t.PO_Date = (
select DATE_FORMAT(STR_TO_DATE(PO_Date, '%m/%d/%Y'), '%Y-%m-%d')
from mytable as i
where i.Pri_ID = (select MAX(Pri_ID) from mytable)
);
I have tried changing the table alias multiple times, consistently get the error that the alias t does not exist.
If anyone stumbles across this in the future, here is what corrected the issue.
update mytable as t
set t.PO_Date = DATE_FORMAT(STR_TO_DATE(PO_Date, '%m/%d/%Y'), '%Y-%m-%d')
ORDER BY t.Pri_ID DESC LIMIT 1;

MySQL - update the row with the highest start date

I am trying to update the member row with the highest start date using:
UPDATE at_section_details a
SET a.sd_end_date = ?
, a.sd_details = ?
WHERE a.cd_id = ?
AND a.sd_start_date = (SELECT MAX(b.sd_start_date)
FROM at_section_details b
WHERE b.cd_id = ?)
The error message is:
"SQLException in updateYMGroup: java.sql.SQLException: You can't specify target table 'a' for update in FROM clause
The table structure is:
sd_id - primary key
cd_id - foreign key (many occurrences)
sd_section
sd_pack
sd_start_date
sd_end_date
sd_details
A member (cd_id) can start and then transfer out.
The member can then transfer in again (new start date). When they transfer out we want to pick up the max start date to transfer out against.
Any assistance would be greatly appreciated.
Regards,
Glyn
You should be able to use the LIMIT statement with an ORDER BY. Something along these lines:
UPDATE at_section_details a
SET a.sd_end_date=?, a.sd_details=?
WHERE a.cd_id=?
ORDER BY a.sd_start_date DESC
LIMIT 1
As it says on this post MySQL Error 1093 - Can't specify target table for update in FROM clause
In My SQL you can't have an update with the same table you are updating inside a subquery.
I would try to change your sub query to some like this
(Select x.* from (select max...) as x)
Sorry for abbreviating the code, I'm on mobile.
This query should work:
UPDATE at_section_details
JOIN (
SELECT cd_id, MAX(sd_start_date) sd_start_date
FROM at_section_details
WHERE cd_id = ?
GROUP BY cd_id
) AS t2 USING (cd_id, sd_start_date)
SET sd_end_date=?, sd_details=?;
See this SQL Fiddle for an example
You can try this mate:
UPDATE at_section_details SET sd_end_date = <input>, sd_details = <input>
WHERE cd_id IN (
SELECT cd_id FROM at_section_details
WHERE cd_id = <input>
ORDER BY sd_start_date DESC
LIMIT 1
);