ON Duplicate Key for exact combination of multiple values - mysql

Let's assume we have this table:
--------------------------
| x | y | z | data |
---------------------------
| 3 | 53 | 24 | hello |
---------------------------
-
Now I only want to update "data" in case there is the exact combination of X, Y, Z.
INSERT INTO TABLE SET x=?,y=?,z=?,data=? ON DUPLICATE KEY UPDATE data=?
This obviously doesn't work. How would I do this?

You only add a composite unique key over the three fields x,y,z. the it works.
You can also use this syntax:
INSERT INTO TABLE (x,y,z,data) values (?,?,?,?) ON DUPLICATE KEY UPDATE data=?;

Related

Use ON DUPLICATE KEY UPDATE to update row with MySQL

I've the following table structure:
Table ___Availabilities :
|--------|------------|------------|------------|
| AVA_Id | AVA_RoomId | AVA_Date | AVA_Status |
|--------|------------|------------|------------|
| 1 | 47 | 2019-03-11 | NoData |
| 2 | 48 | 2019-03-22 | Book |
| 3 | 48 | 2019-03-23 | Book |
|--------|------------|------------|------------|
I want to UPDATE the AVA_Status only if AVA_RoomId and AVA_Date are know in a row of the table. If not, INSERT a new row.
So my query is this one:
INSERT INTO ___Availabilities
(AVA_Id, AVA_RoomId, AVA_Date, AVA_Status)
VALUES('', '103', '2019-04-04', 'Open')
ON DUPLICATE KEY UPDATE
`AVA_RoomId`='47',
`AVA_Date`='2019-03-11'
But it doesn't work as it added a new row whereas it should update the row where AVA_RoomId = 47 and AVA_Date = 2019-03-11.
Why my query is not working please ?
Thanks.
Assuming there is a UNIQUE INDEX(AVA_RoomId, AVA_Date)
You might want to update the AVA_Status when the combination of room and date already exists:
INSERT INTO ___Availabilities
(`AVA_RoomId`, `AVA_Date`, `AVA_Status`)
VALUES('103', '2019-04-04', 'Open')
ON DUPLICATE KEY UPDATE
`AVA_Status` = VALUES(`AVA_Status`),
As #Barmar already explained, if not already done, you can still add an index after table creation:
ALTER TABLE `___Availabilities`
ADD UNIQUE INDEX `uq_room_date` (`AVA_RoomId`, `AVA_Date`)
;

Mysql insert on duplicate key update with where alternative

I would like to know if there is a way to use insert into...on duplicate key without use as parameter the primary key of the table, for example I have the following table :
+----+------------+------+--------+
| id | product_id | year | name |
+----+------------+------+--------+
| 1 | 5 | 2018 | Sword |
| 2 | 7 | 2018 | Shield |
+----+------------+------+--------+
I have an array for insert in that database (PHP - PDO).
This array have 3 items :
the sword
the shield
and the potion
For example, with id as primary key, I would like to know if its possible to perform something like INSERT ... ON DUPLICATE KEY UPDATE
instead using the id (because in this moment I'll not have the id)
I can use a where or something like that, cross the product_id and the year would be perfect.

MySql - apply same unique constraint to two different combinations of fields

How do I specify unique constraint on different combinations of fields? E.g.
id | Fld1 | Fld2 | Fld3
-------------------------
1 | A | B | C
-------------------------
2 | A | C | D
I'd like to make the example above illegal because combination (Fld1, Fld3) in row 1 has same values as combination (Fld1, Fld2) in row 2.
Is there any way to do this?
There is no way possible for this except a trigger but performance will be a constraint. You can only have combine unique key but this case will not fullfil.

How to increment a field in MySql using “ON DUPLICATE KEY UPDATE” when each row increment each value?

this question like increment a field with same value
but I want to achieve each row increment each value
for example:
a is a primary key
Original data
a | share_count | read_count |
1 | 2 | 3 |
through
INSERT INTO table (a, share_count,read_count)
VALUES(1,share_count+1,read_count+2),(2,share_count+2,read_count+3)
ON DUPLICATE KEY UPDATE
a=VALUES(a),share_count=VALUES(share_count),read_count=VALUES(read_count)
Goal result
a | share_count | read_count |
1 | 3 | 5 |
2 | 2 | 3 |
I tried,but fail.Thanks for answering
Usually if you try to INSERT you insert count=1 but not share_count+2 or read_count+3.
If I guess your goal correctly you need something like:
http://sqlfiddle.com/#!9/9d4c6/1
INSERT INTO t1 (a, share_count,read_count)
VALUES
(1,1,1),
(2,1,1)
ON DUPLICATE KEY UPDATE
share_count=share_count+1,read_count=read_count+1

MySQL: UPDATE ... ON DUPLICATE KEY "ANOTHER UPDATE"?

I've stumbled on a problem which is supposedly should have a valid solution in a single MySQL query, yet I can't figure it out (I'm far from an expert in MySQL, alas).
The table has the following columns: A, B, C. (A, B) is a primary key (INTs). C - is a string. The table is populated with records.
Under some conditions I need to update a value x in column A to new value y. If some records do already have y in A (can be many at once, for records with different values in B), "duplicate key(s)" condition occurs. In such case, I need to concatenate values in C from old records and corresponding pending updates, for every specific b.
Example:
| A | B | C
| 1 | 2 | ABC
| 1 | 4 | DEF
| 2 | 2 | GHI
| 2 | 4 | JKL
| 2 | 5 | MNO
After update in A as 2 -> 1, I need to get:
| A | B | C
| 1 | 2 | ABCGHI
| 1 | 4 | DEFJKL
| 1 | 5 | MNO
There is a well-known INSERT ... ON DUPLICATE KEY UPDATE statement. But how to handle gracefully the same problem on updates? I'm not sure if and how REPLACE statement or REPLACE function can be used for the task.
Thanks in advance.
I think, whatever happens, you will need two statements:
ALTER TABLE myTable ADD UNIQUE INDEX (A, B);
INSERT INTO myTable (A, B, C)
SELECT 1, B, C
FROM myTable t
WHERE A = 2
ON DUPLICATE KEY UPDATE
C = CONCAT(myTable.C, VALUES(C))
;
DELETE FROM myTable WHERE A = 2;
See it on sqlfiddle.
If there is a risk of concurrency issues, you will obviously need to perform these within a transaction to preserve atomicity.