Delete row if exists - mysql

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.

Related

mySQL: Where should i put my WHERE?

I'm trying to add a WHERE clause.
How should i manage it?
This is my Query:
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr
SET usr_time_reg.usr_employment = users.employment;
I'm trying to add: WHERE usr_time_reg.usr_emplyoment = "Övrigt"
I've tried:
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr,
usr_time_reg.usr_employment = "Övrigt"
SET usr_time_reg.usr_employment = users.employment;
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr
SET usr_time_reg.usr_employment = users.employment
WHERE usr_time_reg.usr_employment = "Övrigt";
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr
WHERE usr_time_reg.usr_employment = "Övrigt"
SET usr_time_reg.usr_employment = users.employment;
UPDATE usr_time_reg
JOIN users ON usr_time_nr = usr_time_reg.usr_time_nr
AND usr_time_reg.usr_emplyment = "Övrigt"
SET usr_time_reg.usr_employment = users.employment;
Try below as it missing table reference in ON
UPDATE usr_time_reg
JOIN users ON users.usr_time_nr = usr_time_reg.usr_time_nr
SET usr_time_reg.usr_employment = users.employment
WHERE usr_time_reg.usr_employment = "Övrigt";
With JOIN
UPDATE usr_time_reg as tr, users as ur
SET tr.usr_employment = ur.employment
WHERE ur.usr_time_nr = tr.usr_time_nr and tr.usr_employment = "Övrigt";
While joining tables add table alias before field name to avoid ambiguous column error
The following query should work:
UPDATE usr_time_reg u1
INNER JOIN users u2
ON u1.usr_time_nr = u2.usr_time_nr
SET u1.usr_employment = u2.employment;
WHERE u1.usr_emplyment = 'Övrigt';
It can be difficult to keep track of update join syntax for MySQL, in addition to other databases you might be using. So it's always a good idea to have a good reference to use in case you forget.
The main change I made, other than using correct syntax, was to introduce table aliases to the update query. In addition to making it a lot easier to read, it also resolved one of your errors regarding an ambiguous column reference to user_time_nr. Now, it is clear to which table we are referring when we use that column.

Merge 2 tables in 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")

Update table based on value inside this table

I'm trying to update a tablecolumn based on another value inside this table but I keep getting an error.
My query is as follows:
UPDATE partcreditor
SET partcreditor.creditorid = creditor.creditorid
FROM partcreditor
INNER JOIN creditor ON partcreditor.creditornr = creditor.creditornr
WHERE creditor.relgroupid = 1
AND creditor.creditortypeid = 1
UPDATE partcreditor AS PC
INNER JOIN creditor AS CR ON PC.creditornr = CR.creditornr
SET PC.creditorid = CR.creditorid
WHERE CR.relgroupid = 1 AND CR.creditortypeid = 1
No need to use the FROM clause in the update. As well as use alias name for better readability.

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.

SQL Update Join error

I have had a read through a few of the other issues around joining on SQL Updates but haven't been able to finalise a query.
System is all in MySQL (INNODB table structure)
We are wanting to update an amount in one table that will increase an amount based on 2 variables from another table. There are a few constraints that need to be checked in the update to make sure the variables from the second table match the keys in the table to be updated
UPDATE as1
SET as1.amount = as1.amount + (b1.workers * b1.level)
FROM account_stock AS as1
INNER JOIN building AS b1 ON as1.accountID = b1.accountID
INNER JOIN building_seed AS bs1 ON bs1.buildingID = b1.buildingID
WHERE bs1.stockID = as1.stockID
AND b1.accountID = as1.accountID
AND b1.locID = as1.locID
AND b1.status = active
AND b1.gTime > 0
It's getting an error and I can't pick it. Sorry if it is a simple question, all my SQL is self taught so some of the habits I have aren't very good!
MySQL syntax for UPDATE is different. There is no FROM:
UPDATE
account_stock AS as1
INNER JOIN building AS b1 ON as1.accountID = b1.accountID
INNER JOIN building_seed AS bs1 ON bs1.buildingID = b1.buildingID
SET as1.amount = as1.amount + (b1.workers * b1.level)
WHERE bs1.stockID = as1.stockID
AND b1.locID = as1.locID
AND b1.status = active
AND b1.gTime > 0 ;
--- removed duplicate :
--- AND b1.accountID = as1.accountID
Also: is active a column or you meant to write?: AND b1.status = 'active'