Merge 2 tables in MySQL - mysql

I would like to merge to tables in MySQL. In SQL I would use the 'MERGE' command, but what is the equivalent command in MySQL? Lets say i have 3 columns in both tables. Then i want to match the rows by the first column, and if there is a match it needs to update 2nd column but keep the original 3rd column and if there isnt a match then it needs to insert the new row.
Here is the SQL code I would like to convert to MySQL.
MERGE [Synsbasen].[dbo].[Koeretoej] AS T
USING [Synsbasen].[dbo].[KoeretoejLoad] AS S ON (T.KoeretoejIdent = S.KoeretoejIdent)
WHEN NOT MATCHED BY TARGET
THEN INSERT(KoeretoejIdent, KoeretoejArtNavn, KoeretoejAnvendelseNavn, RegistreringNummerNummer, KoeretoejOplysningStatus, KoeretoejOplysningFoersteRegistreringDato, KoeretoejOplysningStelNummer, KoeretoejMaerkeTypeNavn, KoeretoejModelTypeNavn, KoeretoejVariantTypeNavn, DrivkraftTypeNavn, SynResultatSynsType, SynResultatSynsDato, SynResultatSynStatusDato, SidsteSynTjek)
VALUES(S.KoeretoejIdent, S.KoeretoejArtNavn, S.KoeretoejAnvendelseNavn, S.RegistreringNummerNummer, S.KoeretoejOplysningStatus, S.KoeretoejOplysningFoersteRegistreringDato, S.KoeretoejOplysningStelNummer, S.KoeretoejMaerkeTypeNavn, S.KoeretoejModelTypeNavn, S.KoeretoejVariantTypeNavn, S.DrivkraftTypeNavn, S.SynResultatSynsType, S.SynResultatSynsDato, S.SynResultatSynStatusDato, CONVERT(VARCHAR(10),'1900-01-01',110))
WHEN MATCHED
THEN UPDATE SET
T.KoeretoejArtNavn = S.KoeretoejArtNavn,
T.KoeretoejAnvendelseNavn = S.KoeretoejAnvendelseNavn,
T.RegistreringNummerNummer = S.RegistreringNummerNummer,
T.KoeretoejOplysningStatus = S.KoeretoejOplysningStatus,
T.KoeretoejOplysningFoersteRegistreringDato = S.KoeretoejOplysningFoersteRegistreringDato,
T.KoeretoejOplysningStelNummer = S.KoeretoejOplysningStelNummer,
T.KoeretoejMaerkeTypeNavn = S.KoeretoejMaerkeTypeNavn,
T.KoeretoejModelTypeNavn = S.KoeretoejModelTypeNavn,
T.KoeretoejVariantTypeNavn = S.KoeretoejVariantTypeNavn,
T.DrivkraftTypeNavn = S.DrivkraftTypeNavn,
T.SynResultatSynsType = S.SynResultatSynsType,
T.SynResultatSynsDato = S.SynResultatSynsDato,
T.SynResultatSynStatusDato = S.SynResultatSynStatusDato;

Look at: https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
Your query should be something like this:
INSERT into Koeretoej
(KoeretoejIdent, KoeretoejArtNavn, KoeretoejAnvendelseNavn,
RegistreringNummerNummer, KoeretoejOplysningStatus,
KoeretoejOplysningFoersteRegistreringDato, KoeretoejOplysningStelNummer,
KoeretoejMaerkeTypeNavn, KoeretoejModelTypeNavn, KoeretoejVariantTypeNavn,
DrivkraftTypeNavn, SynResultatSynsType, SynResultatSynsDato,
SynResultatSynStatusDato, SidsteSynTjek)
SELECT
S.KoeretoejIdent, S.KoeretoejArtNavn, S.KoeretoejAnvendelseNavn,
S.RegistreringNummerNummer, S.KoeretoejOplysningStatus,
S.KoeretoejOplysningFoersteRegistreringDato,
S.KoeretoejOplysningStelNummer, S.KoeretoejMaerkeTypeNavn,
S.KoeretoejModelTypeNavn, S.KoeretoejVariantTypeNavn,
S.DrivkraftTypeNavn, S.SynResultatSynsType, S.SynResultatSynsDato,
S.SynResultatSynStatusDato, DATE_FORMAT("19000101","%m-%d-%Y")
FROM KoeretoejLoad S LEFT JOIN Koeretoej T ON
T.KoeretoejIdent = S.KoeretoejIdent
ON DUPLICATE KEY UPDATE
KoeretoejArtNavn=S.KoeretoejArtNavn,
KoeretoejAnvendelseNavn=S.KoeretoejAnvendelseNavn,
RegistreringNummerNummer=S.RegistreringNummerNummer,
KoeretoejOplysningStatus=S.KoeretoejOplysningStatus,
KoeretoejOplysningFoersteRegistreringDato=S.KoeretoejOplysningFoersteRegistreringDato,
KoeretoejOplysningStelNummer=S.KoeretoejOplysningStelNummer,
KoeretoejMaerkeTypeNavn=S.KoeretoejMaerkeTypeNavn,
KoeretoejModelTypeNavn=S.KoeretoejModelTypeNavn,
KoeretoejVariantTypeNavn=S.KoeretoejVariantTypeNavn,
DrivkraftTypeNavn=S.DrivkraftTypeNavn,
SynResultatSynsType=S.SynResultatSynsType,
SynResultatSynsDato=S.SynResultatSynsDato,
SynResultatSynStatusDato=S.SynResultatSynStatusDato,
SidsteSynTjek=DATE_FORMAT("19000101","%m-%d-%Y")

Related

Mysql Update from multiple table

I have a query like this:
UPDATE `examination_lower`
SET `Mathematics`=Mathematics.Admno,
`English`=English.total,
`Kiswahili`=Kiswahili.total,
`Science`=Science.total,
`Religion`=Religion.total,
`Social_studies`=Social_studies.total,
`Total`=0
WHERE examination_lower.admno=Admno
Mathematics, English, Kiswahili, etc. are all tables with total and admno as common columns.
How can I get this to work?
You should be able to do something like this: (skipping some columns for brevity)
UPDATE examination_lower, Mathematics, English, Kiswahili -- (continue on)
SET examination_lower.Mathematics = Mathematics.total,
examination_lower.English = English.total,
examination_lower.Kiswahili = Kiswahili.total
WHERE examination_lower.admno = Mathematics.admno
AND examination_lower.admno = English.admno
AND examination_lower.admno = Kiswahili.admno

Can't update two tables in one Query

I've got two Queries to Update two tables:
First Table
UPDATE user_info SET `location` = ".$locationid.", `looking_for` = ".$lookingfor." WHERE `user_info`.`user_id` = ".$infoid.";
Second Table
UPDATE user_personality SET `personality` = '".$changedescription."' WHERE `user_personality`.`user_info_id` = ".$infoid.";
And I'm trying to merge those two Queries, using the same statement.
UPDATE user_info, user_personality
SET user_info.location = ".$locationid.", user_info.`looking_for` = ".$lookingfor.", user_personality.personality = '".$changedescription."'
WHERE `user_info`.`user_id` = ".$infoid."
AND `user_personality`.`user_info_id` = ".$infoid."
I'm not receiving any error message, but is not updating.
What am I doing wrong?
Thanks.
Just a guess...
"
UPDATE user_info i
JOIN user_personality p
ON p.user_info_id = i.user_id
SET i.location = $locationid
, i.looking_for = '$lookingfor'
, p.personality = '$changedescription'
WHERE i.user_id = $infoid;
";
If you set the 2 table fields equal to each other in the where clause it should work, so I believe you'd change your where clause to:
WHERE `user_info`.`user_id` = `user_personality`.`user_info_id`
AND `user_info`.`user_id` = ".$infoid."
MySQL definitely supports updating multiple tables, so the where clause that works for a multi table select statement should also work for an update.

Delete row if exists

DELETE IF EXIST `#__menu`.*
FROM `#__menu`
LEFT JOIN `#__extensions` ON `#__extensions`.`name` = 'com_view'
WHERE `#__menu`.`component_id` = `#__xtensions`.`extension_id`
AND `#__menu`.`alias` = 'view-sites' AND `#__menu`.`path` = 'view-sites' AND `#__menu`.`title` = 'View sites';
What is wrong in my sql? I think the problem is in IF EXIST, but i could not figure out how to use it on row.
When you're deleting rows from a table, you don't need to use IF EXISTS - you're using a WHERE clause, so if it exists - it will be deleted.
Try changing your query to:
DELETE
FROM `#__menu`
LEFT JOIN `#__extensions` ON `#__extensions`.`name` = 'com_view'
WHERE `#__menu`.`component_id` = `#__xtensions`.`extension_id`
AND `#__menu`.`alias` = 'view-sites' AND `#__menu`.`path` = 'view-sites' AND `#__menu`.`title` = 'View sites';
Also, you don't need to specify ```#__menu.*`` (the columns) to be deleted - you'll just needDELETE FROM...`. Check out here for more info regarding the syntax.

Merge not inserting. No error

Can someone tell me why this insert is failing but not giving me an error either? How do I fix this?
merge table1 as T1
using(select p.1,p.2,p.3,p.4,p.5 from #parameters p
inner join table1 t2
on p.1 = t2.1
and p.2 = t2.2
and p.3 = t2.3
and p.4 = t2.4) as SRC on SRC.2 = T1.2
when not matched then insert (p.1,p.2,p.3,p.4,p.5)
values (SRC.1,SRC.2,SRC.3,SRC.4,SRC.5)
when matched then update set t1.5 = SRC.5;
The T1 table is currently empty so nothing can match. The parameters table does have data in it. I simply need to modify this merge so that it checks all 4 fields before deciding what to do.
You can't select from a variable: from #parameters
See the following post: Using a variable for table name in 'From' clause in SQL Server 2008
Actually, you can use a variable table. Check it out:
MERGE Target_table AS [Target]
USING #parameters AS [Source]
ON (
[Target].col1 = [Source].col1
AND [Target].col2 = [Source].col2
AND [Target].col3 = [Source].col3
AND [Target].col4 = [Source].col4
)
WHEN NOT MATCHED BY TARGET
THEN INSERT (col1,col2,col3,col4,col5)
VALUES (
[Source].col1
,[Source].col2
,[Source].col3
,[Source].col4
,[Source].col5
)
WHEN MATCHED
THEN UPDATE SET [Target].col5 = [Source].col5;

How to use concat() to append to multiple columns in a single mysql update

I can't figure out the syntax for Mysql update with multiple concatinations. I want to be able to append a string to the end of the string stored in the database but do it to multiple columns all at once. I can do one column at a time just fine with this
UPDATE `table1`.`column1` SET `category1` = CONCAT(category1,'$value[0]',) WHERE `id`='$id';
But when I try to do it to multiple columns in the same table I get a syntax error.
UPDATE `table1`.`column1`
SET `category1` = CONCAT(category1,'5'),
`category2` = CONCAT(category2,'5'),
`category3` = CONCAT(category3,'5'),
`category4` = CONCAT(category4,'5'),
`category5` = CONCAT(category5,'5'),
`comments` = CONCAT(comments, 'jfsaklfsad')
WHERE `for_student_id`='46';
"You have an error in your SQL syntax;"
I can't find the syntax for separating each concat.
According to MySQL docs, UPDATE does not support such syntax. You must reference the table name, without the column, before the SET:
UPDATE `table1`
SET `category1` = CONCAT(category1,'5'),
`category2` = CONCAT(category2,'5'),
`category3` = CONCAT(category3,'5'),
`category4` = CONCAT(category4,'5'),
`category5` = CONCAT(category5,'5'),
`comments` = CONCAT(comments, 'jfsaklfsad')
WHERE `for_student_id`='46';