MySql using user defined variables in an update statement - mysql

How can I use user defined variables in a MySql update statement? By user defined variables, i'm referring to the #v:=.
The statement below works fine without the user defined variables, but not with them.
update amga a left join amgb b on a.itemTempId = b.itemTempId
set #i:= concat(a.itemCountry,'-',a.id), a.itemId = #i, b.itemId = #i
where a.itemId is null or b.itemId is null;
I'll be using this with php PDO later.
This works, but does use user defined variables
update amga a left join amgb b on a.itemTempId = b.itemTempId
set a.itemId = concat(a.itemCountry,'-',a.id), b.itemId = concat(a.itemCountry,'-',a.id)
where a.itemId is null or b.itemId is null;

Try following syntax:
update amga a left join amgb b on a.itemTempId = b.itemTempId
set a.itemId = (#i:= concat(a.itemCountry,'-',a.id)), b.itemId = #i
where a.itemId is null or b.itemId is null;
SQLFiddle demo

Related

How can I update a column value of a table in DB1 with a column value from a table in DB2?

I'm trying to perform a simple update in SQL between 2 tables from different DB's. The challenge is that in order for the value to be updated it must meet certain conditions. I have used the join statements to meet the conditions and when I go to test the value from table B it is not being updated into table A. Here is what I've done so far.
USE [dbo]
GO
CREATE PROCEDURE
(
#User_ID = INT,
#Batch_ID VARCHAR(32)
)
DECLARE #locid int
SELECT #locid
FROM OtherDB.dbo.User AS UL
WHERE UL.User_ID = #User_Id
and User_Type = 1;
UPDATE M
SET
M.Number = W.Number
FROM dbo.tableA AS W
JOIN dbo.tableB AS B ON B.ID = W.ID
JOIN dbo.tableC AS C ON C.ToolA = B.ToolA
JOIN dbo.tableD as D ON D.Zone = W.Zone_Name
JOIN OtherDB.dbo.tableMax AS M ON M.LID = #locid
AND M.Tool = C.Other_Tool
AND M.Zone = D._Other_Zone
AND M.Station = W.Station
WHERE W.User_ID = #User_ID
AND W.Batch_ID = #Batch_ID
SET NOCOUNT OFF;
The update statement is updating table M in otherDB, not table A on the current database.
You might revise the stored procedure to be
Update A
Set A.Number = W.Number
From dbo.tableA A
....

subquery returns more than 1 value this is not permitted when the subquery follows

I don't know why my case code is having an error but when I execute it not using a parameter by replacing the #PersonId by 2 it runs well. I try to exchange2x the JOIN but I am still having an error. Anyone knows?.
case when ISNULL(dbo.EducationalBackground.SchoolId,'') = '' then
(Select a.SchoolName
from dbo.EducationalBackground as A INNER JOIN
dbo.PersonEducationalBackground as B on a.EducationalBackgroundId = b.EducationalBackgroundId INNER JOIN
dbo.EducationLevel as C on a.EducationalLevelId = c.EducationLevelId
where b.PersonId = #PersonId and a.EducationalLevelId in (2,3))
else (select dbo.school.SchooldName from dbo.School INNER JOIN dbo.EducationalBackground
ON dbo.School.SchoolId = dbo.EducationalBackground.SchoolId INNER JOIN
dbo.PersonEducationalBackground ON dbo.EducationalBackground.EducationalBackgroundId = dbo.PersonEducationalBackground.EducationalBackgroundId
where dbo.PersonEducationalBackground.PersonId = #PersonId)
Cheers. Thanks.

Update: You can't specify target table 'table' for update in FROM clause

I have two queries for oracle. And i need to modify them for mysql.
First query:
UPDATE tec_onoff_file a
SET emailtype = 'MIGR'
WHERE EXISTS
(SELECT 1
FROM tec_onoff_file
WHERE emailtype = 'MIGR'
AND a.acctnbr = acctnbr
AND a.magabbr = magcodes);
I changed it to
update tec_onoff_file t1
join tec_onoff_file t2
on t2.emailtype='MIGR'
and t1.acctnbr=t2.acctnbr
and t1.magabbr=t2.magcodes
set t1.emailtype='MIGR';
and it works.
But second query is harder for me
update tec_onoff_file a
set emailtype = 'REIN'
where transtype = 'REIN'
and curracctnbr not in (select curracctnbr from tec_onoff_file b
where emailtype ='RENW'
and a.curracctnbr=b.curracctnbr);
Could someone help with it? I'm trying change it like first query with JOIN, but it fails, i don't know how to do it.
UPDATE tec_onoff_file a
LEFT
JOIN tec_onoff_file n
ON b.curracctnbr = a.curracctnbr
AND b.emailtype ='RENW'
SET emailtype = 'REIN'
WHERE a.transtype = 'REIN'
AND b.transtype IS NULL;

MySQL UPDATE script not working

I am trying to run an UPDATE script across two tables, but it isn't working. Can anyone tell me what I'm doing wrong:
UPDATE adb_addressbook a, a_table b
SET a.gtxr2_product_family = b.product_family,
SET a.gtxr2_product_family_factory = b.factory,
SET a.gtxr2_product_family_model = b.model,
SET a.gtxr2_product_family_size = b.size
WHERE a.contact_id = b.contact_id;
This is how it should be
update adb_addressbook a
JOIN a_table b on b.contact_id = a.contact_id
SET a.gtxr2_product_family = b.product_family,
a.gtxr2_product_family_factory = b.factory,
a.gtxr2_product_family_model = b.model,
a.gtxr2_product_family_size = b.size;
DEMO
Your syntax is wrong.. It should be something like that
UPDATE A
SET A.NAME = B.NAME
FROM TableNameA A, TableNameB B
WHERE A.ID = B.ID

SQL calculating time between assignments

I have to write an SQL statement which contain a field that contain two different values consecutively but in the way I have wrote it, it return always null because it is interpreted as having the two value in the same time!
My conditions should be : (ci.field = 'Group' and ci.oldString = 'Triage' ) and (ci.field='assignee' and ci.newString is not NULL)
That means calculate time between: when the issue is assigned to group named Triage and when the issue is assigned to a person.
How can I fix it?
My SQL statement:
select TIMEDIFF(a.created,b.created)
from
(select g.created, g.issueid as groupid1
from changegroup g
join changeitem ci on (ci.groupid = g.id)
join jiraissue ji on (ji.id = g.issueid)
join project p on (p.id = ji.project)
join priority pr on (pr.id = ji.priority)
where ci.field = 'Group'
and ci.oldString = 'Triage'
and ci.field='assignee'
and ci.newString is not NULL
and p.pname = 'Test'
and pr.pname='P1'
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00'
) a
left join (
select ji.created, ji.id as groupid2
from jiraissue ji
join changegroup g on (g.issueid = ji.id)
join project p on (p.id = ji.project)
where p.pname = 'Test'
and ji.created between '2011-08-11 14:01:00' and '2011-08-12 14:11:00'
) b ON (a.groupid1 = b.groupid2);
This is the table from which I should retrieve data
See my comment about the quality of your question but a hint at how to solve this goes like (assuming you can make sure this doesn't create 1-n joins)
select groupid_orsomething_else, TIMEDIFF(a.created, b.created)
from yourtable
left join
(select groupid_orsomething_else, created
from yourtable
where field = 'Group' and oldstring is 'Triage'
) a
on a.groupid_orsomething_else = yourtable.groupid_orsomething_else
left join
(select groupid_orsomething_else, created
from yourtable
where field = 'assignee' and oldstring is null) b
on b.groupid_orsomething_else = yourtable.groupid_orsomething_else