MySQL update multiple table for kunde and profil - mysql

I have a table "profil" and "kunde". "profil" got the email and password. "kunde" got the bio, username and email as index from "profil". "kunde" got the primary key username.
I want to update the username bio and passwort at once
UPDATE profil, kunde SET kunde.username = ?, kunde.bio = ?, profil.passwort = ? WHERE profil.email = ?;
the error code i got
#1062 - duplicated entry 'Kneipengänger60' for key 'PRIMARY'

You cannot update two tables in a single statement. However you can use transactions to ensure that the two statements are updated as a single unit. If one fails both are rolled back.
BEGIN TRANSACTION;
UPDATE profil
SET profil.passwort = ?
FROM profil, kunde
WHERE profil.id = kunde.id //Here you use the secondary key and primary keys to link the tables
and profil.email = ?;
UPDATE kunde
SET kunde.username = ?, kunde.bio = ?
FROM profil, kunde
WHERE profil.id = kunde.id //Here you use the secondary key and primary keys to link the tables
and profil.email = ?;
COMMIT;

Related

how to update with , seprator with preivous value in mysql

$r ='insert into posts set id="'.$id.'", postId="'.$idus.'" ON DUPLICATE KEY UPDATE whoLikes= whoLikes,$idus'
like := ON DUPLICATE KEY UPDATE new = new+'$new'
but i need:= ON DUPLICATE KEY UPDATE new = new,'$new'
You should be using MySQL concat() function:
... ON DUPLICATE KEY UPDATE `whoLikes`=CONCAT(`whoLikes`, $idus)

MySQL Workbench: Error Code 1452. Cannot add or update a child row: a foreign key constraint fails

I'm still fairly new to SQL. I'm updating a DB and I came across this message. The problem is, I've already executed this insert before but had to delete it due to me entering the same address 3 times instead of once.
Can anybody help me, I don't understand what is wrong:
> insert into ort
(plz, name) values
('4900', 'Langenthal')
;
>insert into adresse
(strasse, strassennr, ortID) values
('Eisenbahnstrasse', '7', (select oid from ort where name = 'Langenthal' and plz='4900'))
;
>
insert into liegenschaft
(liegenschafttypid, adressid) values
((select ltypid from liegenschaft_typ where name = 'Wohnhaus / Firma'), (select oid from ort where name = 'Langenthal' and plz = '4900'))
;
I keep on getting this message:
> 0 16 14:09:25 insert into liegenschaft (liegenschafttypid, adressid) values
((select ltypid from liegenschaft_typ where name = 'Wohnhaus / Firma'), (select oid from ort where name = 'Langenthal' and plz = '4900')) Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`parking`.`liegenschaft`, CONSTRAINT `FK_adresse` FOREIGN KEY (`adressID`) REFERENCES `adresse` (`AID`)) 0.015 sec
You don't have entry in column adresse.AID for liegenschaft.adressid that you want to insert.
You either specified wrong column in foreign key, insert or you forgot to insert data into that column.
You need to do one of those:
insert into adresse
(strasse, strassennr, AID) values
('Eisenbahnstrasse', '7', (select oid from ort where name = 'Langenthal' and plz='4900'));
or
insert into adresse
(strasse, strassennr, ortID, AID) values
('Eisenbahnstrasse', '7', (select oid from ort where name = 'Langenthal' and plz='4900'), (select oid from ort where name = 'Langenthal' and plz='4900'));
or alter that foreign key to point at ortID instead AID
The column adressid in liegenschaft shall have the key adressID and not oid.
Try (assume that there just one entry for each address)
insert into liegenschaft
(liegenschafttypid, adressid) values
((select ltypid from liegenschaft_typ where name = 'Wohnhaus / Firma'), (select adressID from ort where name = 'Langenthal' and plz = '4900'))

MYSQL ONDUPLICATE KEY UPDATE ISSUE

I am trying to use 'ON DUPLICATE KEY UPDATE' statement to update column when duplicate primary key are already in table.
but even if table has duplicated primary key, it does not update column.
below is 'ON DUPLICATE KEY UPDATE' statement.
is there somthing wrong?
ON DUPLICATE KEY UPDATE authenticated = authenticated
and notAuthenticated = notAuthenticated
and stoped = stoped
and deleted = deleted
and updatedDate = now()
;
use VALUES(Column) and replace all this AND with comma ,:
ON DUPLICATE KEY UPDATE authenticated = VALUES(authenticated),
notAuthenticated = VALUES(notAuthenticated),
stoped = VALUES(stoped),
deleted = VALUES(deleted),
updatedDate = now()
Don't use AND, but use commas:
ON DUPLICATE KEY UPDATE
authenticated = VALUES(authenticated),
notAuthenticated = VALUES(notAuthenticated),
stoped = VALUES(stoped),
deleted = VALUES(deleted),
updatedDate = now()

mysql - conditional insert row within a query with ON DUPLICATE KEY UPDATE

I have a complex query with a ON DUPLICATE KEY Update inside. I only want to insert the last value if there is no row with “timestamp_dag” = 1420070400, if there is a row with that condition I want to do nothing.
INSERT INTO data_prijzen_advertentie (
`ID_advertentie`,`jaar`,`rijnr`,`status_prijs`,`datum_dag`,`timestamp_dag`,
`prijs_maand`,`prijs_week`,`prijs_midweek`,`prijs_langweekend`,`prijs_weekend`,
`prijs_dag`,`prijs_ochtend`,`prijs_middag`
)
VALUES
(100,2014,1,1,'12-05-2014',1399852800,0,100,0,75,0,0,0,0),
(100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0),
(100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0),
(100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0)
ON DUPLICATE KEY UPDATE
status_prijs = VALUES(status_prijs), datum_dag = VALUES(datum_dag),
timestamp_dag = VALUES(timestamp_dag), prijs_maand = VALUES(prijs_maand),
prijs_week = VALUES(prijs_week), prijs_midweek = VALUES(prijs_midweek),
prijs_langweekend = VALUES(prijs_langweekend), prijs_weekend = VALUES(prijs_weekend),
prijs_dag = VALUES(prijs_dag), prijs_ochtend = VALUES(prijs_ochtend),
prijs_middag = VALUES(prijs_middag);
it's the best if you can handle this before the query string creation..
but if you can't, and your exceptional value is static, you can try something like this:
(I haven't tried to run it yet though)
INSERT INTO data_prijzen_advertentie (
`ID_advertentie`,`jaar`,`rijnr`,`status_prijs`,`datum_dag`,`timestamp_dag`,
`prijs_maand`,`prijs_week`,`prijs_midweek`,`prijs_langweekend`,`prijs_weekend`,
`prijs_dag`,`prijs_ochtend`,`prijs_middag`
)
VALUES
(100,2014,1,1,'12-05-2014',1399852800,0,100,0,75,0,0,0,0),
(100,2014,2,1,'23-05-2014',1400803200,0,75,0,101,0,0,0,0),
(100,2014,3,1,'30-05-2014',1401408000,0,100,0,75,0,0,0,0),
(100,2014,4,1,'01-01-2015',1420070400,0,0,0,0,0,0,0,0)
ON DUPLICATE KEY UPDATE
status_prijs = IF(timestamp_dag<>1420070400, VALUES(status_prijs), status_prijs),
datum_dag = IF(timestamp_dag<>1420070400, VALUES(datum_dag), datum_dag),
timestamp_dag = IF(timestamp_dag<>1420070400, VALUES(timestamp_dag), timestamp_dag),
prijs_maand = IF(timestamp_dag<>1420070400, VALUES(prijs_maand), prijs_maand),
prijs_week = IF(timestamp_dag<>1420070400, VALUES(prijs_week), prijs_week),
prijs_midweek = IF(timestamp_dag<>1420070400, VALUES(prijs_midweek), prijs_midweek),
prijs_langweekend = IF(timestamp_dag<>1420070400, VALUES(prijs_langweekend), prijs_langweekend),
prijs_weekend = IF(timestamp_dag<>1420070400, VALUES(prijs_weekend), prijs_weekend),
prijs_dag = IF(timestamp_dag<>1420070400, VALUES(prijs_dag), prijs_dag),
prijs_ochtend = IF(timestamp_dag<>1420070400, VALUES(prijs_ochtend), prijs_ochtend),
prijs_middag = IF(timestamp_dag<>1420070400, VALUES(prijs_middag), prijs_middag);
You will only have the ON DUPLICATE KEY UPDATE ... portion run if the timestamp_dag matches the criteria for triggering it. From the MySQL docs:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that
would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an
UPDATE of the old row is performed.
So assuming your table is built this way, then the update will trigger.
You can add a UNIQUE index using the syntax as described here.
CREATE UNIQUE INDEX ind_unique_timestamp_dag ON data_prijzen_advertentie (timestamp_dag)

Bind MySQL variables when using INSERT INTO ... ON DUPLICATE KEY

I have a simple SQL insertion into a two field MySQL table (records):
Where 'id' is the primary key (int) ... and 'photo' is a mediumblob
$photo = <binaryfile>
$id = recordNo
$MYSQL = "INSERT INTO records (id, photo)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE photo = ?";
$cursor->execute( *what goes in here? I have 3 ?'s but only 2 variables* );
In the execute function you should pass the array like this:
$cursor->execute(array($id,$photo,$photo));
Here $photo name should come two time one is for second one and next is for third ? mark.