The following query works in MySQL:
INSERT INTO `test`.`address_tab1`
(`city`,`street_name`,`pincode`)
SELECT
'mycity1', 'mystreerName1', '4855881'
where (SELECT COUNT(*) FROM `test`.`address_tab1`) = 0;
But the following query does not work, although there are no syntax error. Why so? I would like to insert multiple rows.
INSERT INTO `test`.`address_tab1`
(`city`,`street_name`,`pincode`)
SELECT
('mycity1', 'mystreerName1', '4855881')
where (SELECT COUNT(*) FROM `test`.`address_tab1`) = 0;
I get the following error:
Operand should contain 1 column(s)
When you make a list of expressions with parentheses, this is a tuple. But each item in a select-list must be a scalar expression; they can't be tuples.
Another way of thinking of this is that when using
INSERT ... SELECT, you're inserting the results of theSELECTinto some columns. They are matched up by position.But if you tried to use a tuple in the select-list (even if it were supported, which it isn't) it would count as only one thing, so it would try to insert it into the first column of your
address_tab1table. But inserting a tuple into a single column is not legal, and it would also leave no values for the other two columns in the insert-list.I'm using these markers like
^1only to indicate each column in these examples. These markers are not part of SQL syntax.Re your comment:
I would use a new syntax of MySQL 8.0: the VALUES statement. This allows you to specify multiple rows of literal values.
If you are not using MySQL 8.0 yet, you should upgrade, because support for MySQL 5.x is ending soon.