Insert, select & ON Duplicate KEY - mysql

i'am always getting syntax error when execute this query. Any Ideas what i'm doing wrong?
INSERT INTO cookies (cookie, buy, orders, ordervalue) SELECT o.uid cookie, 'eby' as a, COUNT(o.price ) as b , ROUND( SUM(o.price) , 2 ) umsatz
FROM `Orders` o
WHERE uid != ''
GROUP BY uid ON DUPLICATE KEY UPDATE buy=VALUES(a), orders=VALUES(b);
I'm not sure ift its possible to use alias in "on duplicate". I ve tried also to calculate the values again in the "on duplicate" part. But also get an error.

The problem is here:
... UPDATE buy=VALUES(a), orders=VALUES(b)
^ ^
a and b aren't valid in this context. Reference the name of the column in the column list of the INSERT to get the value that would have been inserted into that column (if the insert had succeeded)
... UPDATE buy=VALUES(buy), orders=VALUES(orders)
^^^ ^^^^^^
(It doesn't matter if the expressions in the SELECT have aliases or not; it's not valid to reference expressions from the SELECT in the ON DUPLICATE KEY clause.

Related

How to update certain column in sql using select and where clause on the same table?

I have a table called employeepostinghistory with the following columns:
employee_posting_id, employee_posting_to, employee_posting_from, emp_emp_cnic
employee_posting_id is primary key and emp_emp_cnic is foreign key
Basically this table is responsible to hold employee history with from and to dates i.e. employee_posting_from and employee_posting_to.
I want to update all records setting employee_posting_to=NULL Where employee history is latest so, I have used DESC on employee_posting_from. But, UPDATE query says subquery return more than 1 record, What can be the possible solution for this problem.
UPDATE employeepostinghistory
SET employeepostinghistory.posting_to=NULL
WHERE employeepostinghistory.emp_emp_cnic=(
SELECT DISTINCT employeepostinghistory.emp_emp_cnic
from employeepostinghistory
GROUP BY employeepostinghistory.emp_emp_cnic
ORDER BY employeepostinghistory.posting_from DESC
)
;
UPDATE employeepostinghistory
NATURAL JOIN (
SELECT emp_emp_cnic, MAX(datetime_column) datetime_column
FROM employeepostinghistory
GROUP BY 1
) last_row_per_employee
SET employeepostinghistory.posting_to=NULL
;
emp_emp_cnic.datetime_column must be unique.

Need assistance with SQL Query with error message Operand should contain 1 column(s)

Good Day
I have the following query but I'm getting an error message 'Operand should contain 1 column(s)'
Any assistance would be greatly appreciated
UPDATE expenditure
SET BP = (
SELECT * ,
SUM(balance_provision - actual_amt_voucher) over (partition by voteid order by expenditureid) AS BalanceProvision
FROM expenditure
)
It looks like you want to update column bp with a window sum.
Your query fails because you are trying to assign a resultset (that has multiple columns) to a single column.
But even you were returning a scalar value from the subquery, this would not work, since MySQL does not allow reusing the target table of the update in a subquery.
Instead, yo can use the update ... join syntax. Assuming that expenditureid is the primary key of the table, as its name suggests, that would be:
update expenditure e
inner join (
select
expenditureid,
sum(balance_provision - actual_amt_voucher) over (partition by voteid order by expenditureid) bp
from expenditure
group by expenditureid
) e1 on e.expenditureid = e1.expenditureid
set e.bp = e1.bp

Mysql Update statement to delete duplicates

I would like to make an update on my unique id like replacing white space, but on that case the update statement breaks because of redundant s I tried to make an update as it follows but
wrong syntax
UPDATE __cat_product
SET product_id = REPLACE(product_id,' ','')
ON DUPLICATE KEY DELETE product_id
WHERE product_id LIKE "%A %"
how to do this in the right way?
There is no ON DUPLICATE KEY syntax that is part of an UPDATE statement.
I'm having difficulty figuring out what you actually want to do.
You want to update a column in a table, to remove all space characters. And when you tried to run that statement, the statement encountered an error like Duplicate entry 'foo' for key 'PRIMARY'.
Assuming that product_id is varchar, try this on for size:
UPDATE __cat_product t
JOIN ( SELECT MAX(s.product_id) AS product_id
, REPLACE(s.product_id,' ','')
FROM __cat_product s
WHERE s.product_id LIKE '%A %'
AND NOT EXISTS ( SELECT 1
FROM __cat_product d
WHERE d.product_id = REPLACE(s.product_id,' ','')
)
GROUP BY REPLACE(s.product_id,' ','')
) r
ON r.product_id = t.product_id
SET t.product_id = REPLACE(t.product_id,' ','')
The inline view aliased as r gets the values of product_id that can actually be updated, because the "updated" product_id value doesn't already exist in the table. And the GROUP BY ensures that we're only getting one product_id per targeted replacement value.
There may be rows in the table that still match the predicate product_id LIKE '%A %'. The spaces cannot be removed from product_id column on those rows without throwing a duplicate key exception.
A separate DELETE statement could be used to remove those rows.

how to use a dynamic inner column outside the select

i have this query and i get error in mysql
Error Code: 1054. Unknown column 'calc_diff_free' in 'field list'
I understand the problem, but how can i use this column without changing too much
I filtered the query to hide some data and to help you guys to read the query better
insert into ranking_1 (difference_free)
(
select
f.ranking, rc.ranking,
(-1*(f.ranking-rc.ranking)) as calc_diff_free
from base_testing.ranking_temp f
left join ranking_1 rc
on f.id=rc.id
where 1
)
on duplicate key update difference_free=calc_diff_free
Thanks for your help!!
The on duplicate key update clause in your query is outside the scope of the select item where the column calc_diff_free is declared.
You may need to recast your query to put it in scope.
try something like this,
insert into ranking_1 (difference_free)
select -1 * (f.ranking-rc.ranking) as calc_diff_free
from base_testing.ranking_temp f
left join ranking_1 rc
on f.id=rc.id
on duplicate key update difference_free = calc_diff_free
Point: you must have equal number of columns in your insert and select clause.
I think i manage to get it working...
insert into ranking_1 (difference_free)
(
select
f.ranking, rc.ranking,
(-1 * (f.ranking-rc.ranking)) calc_diff_free
from base_testing.ranking_temp f
left join ranking_1 rc
on f.id=rc.id
where 1
)
on duplicate key update difference_free=-1*(f.ranking-rc.ranking)
But it's give me a few warnings...
85672 row(s) affected,Records: 42883, Duplicates: 42789, Warnings: 4

subquery in where clause of UPDATE statement

I have the database of ATM card in which there are fields account_no,card_no,is_blocked,is_activated,issue_date
Fields account number and card numbers are not unique as old card will be expired and marked as is_block=Y and another record with same card number ,account number will be inserted into new row with is_blocked=N . Now i need to update is_blocked/is_activated with help of issue_date i.e
UPDATE card_info set is_blocked='Y' where card_no='6396163270002509'
AND opening_date=(SELECT MAX(opening_date) FROM card_info WHERE card_no='6396163270002509')
but is doesn't allow me to do so
it throws following error
1093 - You can't specify target table 'card_info' for update in FROM clause
Try this instead:
UPDATE card_info ci
INNER JOIN
(
SELECT card_no, MAX(opening_date) MaxOpeningDate
FROM card_info
GROUP BY card_no
) cm ON ci.card_no = cm.card_no AND ci.opening_date = cm.MaxOpeningDate
SET ci.is_blocked='Y'
WHERE ci.card_no = '6396163270002509'
That's one of those stupid limitations of the MySQL parser. The usual way to solve this is to use a JOIN query as Mahmoud has shown.
The (at least to me) surprising part is that it really seems a parser problem, not a problem of the engine itself because if you wrap the sub-select into a derived table, this does work:
UPDATE card_info
SET is_blocked='Y'
WHERE card_no = '6396163270002509'
AND opening_date = ( select max_date
from (
SELECT MAX(opening_date) as_max_date
FROM card_info
WHERE card_no='6396163270002509') t
)