On Duplicate Key update - MariaDB - mysql

I have a MySQL Statement to insert data to 4 rows at a time. The insert is working but I'm having difficulty with the ON DUPLICATE KEY UPDATE.
I'm getting an error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''count = VALUES(11, 22, 33, 44)'' at line 15
Here is an example:
INSERT INTO table1 (id, dept, date, count)
VALUES
(1, 4, 2018-01-15, 3),
(2, 3, 2018-01-15, 4),
(3, 3, 2018-01-15, 14),
(4, 2, 2018-01-15, 11)
ON DUPLICATE KEY UPDATE
count = VALUES(11, 22, 33, 44)
I tried to wraps the dept and count update with '' but that didn't help. Is there a better way to update the count on DUPLICATES. Could you please help! Thanks!

The argument to VALUES() is supposed to be the name of a column being inserted into. It will use the value that would have been inserted into that column if there hadn't been a duplication.
INSERT INTO table1 (id, dept, date, count)
VALUES
(1, 4, 2018-01-15, 3),
(2, 3, 2018-01-15, 4),
(3, 3, 2018-01-15, 14),
(4, 2, 2018-01-15, 11)
ON DUPLICATE KEY UPDATE
count = VALUES(count)
If id = 1 already exists, this will set its count to 3 and leave all the other columns unchanged.

Related

MySql Insert multiple and Ignore duplicate records

I have this MySQL INSERT query that adds a Product to multiple categories:
INSERT INTO _categories_products (product_id, category_id) VALUES (1, 14), (1, 8), (1, 1), (1, 22);
This works great, however, if I add the same product to another subcategory from the same parent, it will create duplicate records for the parent categories:
INSERT INTO _categories_products (product_id, category_id) VALUES (1, 14), (1, 8), (1, 1), (1, 23);
Question: What would be the appropriate MySQL query that Ignores the insertion of duplicate records? In other words, the second query should INSERT only one record: 1, 23.
I tried also INSERT IGNORE INTO but nothing changed.
Thank You!
To start with, you want to create a unique constraint on categories/product tuples to avoid duplicates:
alter table _categories_products
add constraint _categories_products _bk
unique (product_id, category_id);
From that point on, an attempt to insert duplicates in the table would, by default, raise an error. You can trap and manage that error with MySQL on duplicate key syntax:
insert into _categories_products (product_id, category_id)
values (1, 14), (1, 8), (1, 1), (1, 23)
on duplicate key update product_id = values(product_id)
In the case of duplicate, the above query performs a dumb update on product_id, which actually turns the insert to a no-op.

INSERT INTO... VALUES with inexisting values [duplicate]

This question already has answers here:
MySQL - ignore insert error: duplicate entry
(5 answers)
Closed 4 years ago.
I have a question about insert data in a table.
My table is just composed of a primary key (that is composed by two foreign key).
The problem is: I don't know how to execute my script to insert all rows except non-existing values. Because in the script, some of the inserts are not available anymore.
For some reason, someone gave me the insert script but with some foreign key that were delete. So when I executed the script, I get an error "Constraint fail..." and it's normal.
But how to insert rows easily to avoid error due to invalid foreign key?
INSERT INTO `X_Y` (`x_id`, `y_id`) VALUES
(4, 1),
(4, 2),
(4, 3),
(4, 4),
(4, 5),
(5, 6),
(5, 7),
(5, 8),
(6, 9),
(6, 10),
(7, 11),
(8, 12),
(8, 13),
(11, 18),
(12, 19),
... ( about 2000 insert)
(1680, 2071);
For example : insert (4,1) can't be executed because foreign key x_id 4 does not exist anymore.
Try first inserting your data into a temporary table, with no constraints:
INSERT INTO temp (x_id, y_id)
VALUES
(4, 1),
(4, 2),
...;
Then, use the following query to insert into your target table:
INSERT INTO X_Y (x_id, y_id)
SELECT x_id, y_id
FROM temp t
WHERE
EXISTS (SELECT 1 FROM X x WHERE x.id = t.x_id) AND
EXISTS (SELECT 1 FROM Y y WHERE y.id = t.y_id);
The above insert checks, for every row, that the specified x_id and y_id values in fact have matching primary keys in the two parent tables.

Selecting id against each field in a table

I have a list of ids as (1,2,3...)
i called it $category_ids. Now in the same table there is a field of super_parent_category which means the highest label of that category in list.
Now i want to retrieve super parent ids for each of the category ids.
SELECT es_super_parent,es_id
FROM ephpb2b_categories
WHERE es_id IN ({$category_ids})");
The above query is returning me only 4 records as all of the ids in $category_ids belongs to these 4 categories.
But i want a result of all 50 records that are in $category_ids.
So if i am supplying 1,2,4,5,6,6,76,
I should return 1,3,4,5,6,66,2,
Thanks
It works for me. Double check your PHP variable $category_ids with a var_dump(), i.e.
I testet it with your provided sample data.
SELECT es_super_parent, es_id
FROM ephpb2b_categories
WHERE es_id IN (1, 2, 4, 5, 6, 6, 76);
and the following table schema:
CREATE TABLE ephpb2b_categories (
es_id INT PRIMARY KEY,
es_super_parent INT NOT NULL,
FOREIGN KEY (es_super_parent) REFERENCES ephpb2b_categories (es_id)
) ENGINE=InnoDB;
And inserted some data:
INSERT INTO ephpb2b_categories (es_id, es_super_parent) VALUES
(0, 0),
(1, 1),
(76, 76),
(2, 76),
(3, 2),
(4, 4),
(5, 5),
(6, 6),
(66, 6);
Here's a working SQL Fiddle

MySQL AES_ENCRYPT all values for a column

Lets assume I have a table with 10,000 rows. What is the fastest way to encrypt every value for a specific column with AES_ENCRYPT using the same AES password/salt?
I actually will have to do this across many tables and many columns, so I am trying to find a quick way to do this.
Assuming that I have changed the type to the city_name column below to BLOB already, is there someway I could import the data back into mysql using a query like this (I am just showing the first 10 inserts, but this table could have 10,000+ of rows):
INSERT INTO `cities` (`id`, AES_ENCRYPT(`city_name` `password`), `state_id`, `country_id`)
VALUES
(1, 'Feyzabad', 15, 1),
(2, 'Baghlan', 17, 1),
(3, 'Nahrin', 17, 1),
(4, 'Pol-e Khomri', 17, 1),
(5, 'Cool urhajo', 18, 1),
(6, 'Farakh', 19, 1),
(7, 'Andkhvoy', 20, 1),
(8, 'Ghazni', 21, 1),
(9, 'Shahrag', 22, 1),
(10, 'Gereshk', 23, 1);

Insert into when number of columns don't match number of values

I have an insert sql statement, e.g.
INSERT INTO `table_a` (`col_a`, `col_b`, `col_c`, `col_d`) VALUES
(1, 2, 3, 4),
(2, 1, 6, 9),
(3, 1, 4, 5)
I want to insert this into another table, however the table I want to insert it into has a different structure to that of the sql statement (it has fewer fields) e.g.
table_b has columns 'col_a', 'col_b', 'col_d'
What do I have to do to the original sql statment so that I can get it to insert it into table_b. I guess it would be something along the lines of just ignoring the value which is in col_c and just sending this to a temp variable rather than a field.e.g.
INSERT INTO `table_b` (`col_a`, `col_b`, #temp_var, `col_d`) VALUES
(1, 2, 3, 4),
(2, 1, 6, 9),
(3, 1, 4, 5)
Use a temporary table:
CREATE TEMPORARY TABLE myTemp (
col_a integer,
col_b integer,
col_c integer,
col_d integer
);
INSERT INTO myTemp (col_a, col_b, col_c, col_d) VALUES (1, 2, 3, 4), (2, 1, 6, 9), (3, 1, 4, 5);
INSERT INTO table_a (SELECT col_a,col_b,col_d FROM myTemp);
The table gets dropped once your session ends (or you can remove it manually)
How about you remove it?
INSERT INTO table_b (col_a, col_b, col_d) VALUES (1, 2, 4), (2, 1, 9), (3, 1, 5)
This is ugly, and I have just tried in in SQLite, but I can image that it also works in MySQL (the documentation doesn't say it's not allowed) (update: see John's comment, it does not work in MySQL):
sqlite> create table t(a,b,c);
sqlite> insert into t (a,b,b,c) values (1,2,3,4);
sqlite> select * from t;
1|2|4