Column count doesn't match value count in MySQL - mysql

Whenever I am trying to insert data in table:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');
MySQL said:#1136 - Column count doesn't match value count at row 1
How can i fix it?
I don't understand what need to do.
TIA

The provided number of columns in the column list specification and number of column values in each record must match.
Assuming you don't want to insert email data, remove that from the column list:
INSERT INTO `operator`(`id`, `operator_name`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');
or pass null for email:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink',null,'This is all about Banglalink'),
(2, 'Robi', null,'This is all about Robi');
Second method is useful when you may have emails for few records.

You pass one item - info:
INSERT INTO `operator`(`id`, `operator_name`, `email`, `info`)
VALUES (1,'Bangalink','This is all about Banglalink', NULL),
(2, 'Robi', 'This is all about Robi',NULL);
or
INSERT INTO `operator`(`id`, `operator_name`, `email`)
VALUES (1,'Bangalink','This is all about Banglalink'),
(2, 'Robi', 'This is all about Robi');

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.

How ignore error when the data is replaced in mySQL?

I insert data into the table as follows:
REPLACE INTO `test`(`id`,`text`) VALUES (1,'first'), (2, 'second'), (3, 'third')
But if one of the sets of data is incorrect, then all the other sets do not fall into the table.
REPLACE INTO `test`(`id`,`text`) VALUES (1,'new first'), (2, NULL), (3, 'new third')
How to achieve the following:
The first and third set of data is to replace the existing data in the table. And the second is to be ignored, and the data in the table should not change.
Try
insert ignore INTO `test`(`id`,`text`) VALUES (1,'new first'), (2, NULL), (3, 'new third')
insert ignore command work as replace command but it convert errors into warning.

"decimal" - Data truncated for column

I'm trying to add some test values into my database, but I'm always getting this Data truncated for column ... error when I enter decimal values.
For example I'm trying to enter this:
INSERT INTO `offers` (`id`, `userid`, `amount`, `onebtc`, `price`, `discount`) VALUES (NULL, '6', '14,22', '0', '10,32', '1');
into this table:
table
Have you tried to sending a decimal instead of a string??
INSERT INTO offers (id, userid, amount, onebtc, price, discount) VALUES (NULL, 6, 14.22, 0, 10.32, 1);
You're sending strings for everthing, so MySQL has to type cast it for you.

Delete old record and insert new ones in one query mysql

Is there a way to delete old records from table and to insert new ones in one query with mysql?
For example to make this 2 querys into one.
DELETE FROM A where customer_id = 5;
INSERT INTO A (product_id, customer_id) VALUES (1, 5);
INSERT INTO A (product_id, customer_id) VALUES (5, 5);
INSERT INTO A (product_id, customer_id) VALUES (12, 5);
INSERT INTO A (product_id, customer_id) VALUES (543, 5);
Delete and insert are not meant to be in one single query. However, you can merge your inserts into a single query. This would be the script:
DELETE FROM A where customer_id = 5;
INSERT INTO A (product_id, customer_id) VALUES (1, 5), (5, 5), (12, 5), (543, 5);

MySQL: Conditional INSERT ... SELECT for multiple rows with constant values

I know how to insert multiple rows with constant values in a single query:
INSERT INTO table
VALUES
(1, 'a', 'x'),
(2, 'b', 'y'),
(3, 'c', 'z');
But how do I do this conditionally? I want to make sure only non-existing* rows get inserted. Is it even possible to do it in a single query?
*A row exists when all its columns are identical to the one we are inserting
Use INSERT IGNORE INTO instead of INSERT INTO. The former will only insert rows, if no unique key constraints are violated.
INSERT IGNORE INTO table
VALUES
(1, 'a', 'x'),
(2, 'b', 'y'),
(3, 'c', 'z');