Operand should contain 1 column(s) while insert in mysql - mysql

I have two tables which are user and info. Need to insert into info table with max id from user table.I tried using below query with single row insert it was working fine. But I try to multiple insert it doesn't. getting error as "#1241 - Operand should contain 1 column(s)"
INSERT INTO user( name, email, password )
values
(
((SELECT MAX( id ) as id from info), 'test1#gmail.com', '123'),
((SELECT MAX( id ) as id from info), 'test1#gmail.com', '123'))

When inserting from a select, we don't need to use VALUES:
INSERT INTO user( name, email, password )
SELECT MAX(id), 'test1#gmail.com', '123' FROM info
UNION ALL
SELECT MAX(id), 'test1#gmail.com', '123' FROM info;
Note also that you probably don't need to wrap the MAX(id) query as a subquery. Instead, just select the constants you want in a single top level query.

Related

How can i insert mutiple row from another database by using values

When I enter this command, there will be an error because there are two rows in tbl_carts where email is ABC#gmail.com. Is there a way to take mutiple row from one database to another.
My sql command is:
INSERT INTO tbl_history(orderid, email, product_id, qty) VALUES
('testing', (SELECT email FROM tbl_carts WHERE email 'ABC#gmail.com'),
(SELECT product_id FROM tbl_carts WHERE email='ABC#gmail.com'),
(SELECT qty FROM tbl_carts WHERE email='ABC#gmail.com'))
Error Message is:
Subquery returns more than 1 row
This is the correct way to do INSERT using SELECT values from other table:
INSERT INTO tbl_history(orderid, email, product_id, qty)
SELECT 'testing', email, product_id, qty FROM tbl_carts WHERE email='ABC#gmail.com';
Here's a reference from the official documentation

Mysql insert from multi source

I need to insert into table with 3 columns, 2 columns from a select and one column external.
Like this:
Insert into customer(username, fullname, rate)
values
((select username, fullname from users), 1500)
It return : column count doesnt match
You want the insert ... select syntax, with a literal value in the third column:
Insert into customer(username, fullname, rate)
select username, fullname, 1500 from users

error with IF EXISTS ()

IF NOT EXISTS (SELECT 1 FROM Products WHERE name='Iphone1' AND manufacturer='appl') THEN
INSERT INTO Products(product_id, name, category, manufacturer)
VALUES (10000, 'IphoneZ', null, 'appl');
ERROR: syntax error at or near "IF" LINE 1: IF NOT EXISTS (SELECT 1
FROM Products WHERE name='Iphone1' A...
Can anyone help me understand what I am doing wrong?
MySQL only supports the IF statement in programming blocks -- stored procedures, functions, and triggers. Hence you cannot do what you want that way.
Instead, you can just write a single query:
INSERT INTO Products (product_id, name, category, manufacturer)
SELECT product_id, name, category, manufacturer
FROM (SELECT 10000 as product_id, 'IphoneZ' as name, null as category, 'appl' as manufacturer) t
WHERE NOT EXISTS (SELECT 1 FROM Products p WHERE p.name = t.name and p.manufacturer = t.manufacturer);
Actually, though, it is best to have the database directly enforce this sort of uniqueness. You can do so with a unique constraint/index:
CREATE UNIQUE INDEX unq_product_manufacturer_name ON product(manufacturer, name);
Then you can write an query to ignore errors by doing:
INSERT INTO Products (product_id, name, category, manufacturer)
VALUES (10000, 'IphoneZ', null, 'appl')
ON DUPLICATE KEY UPDATE category = VALUES(category);
The ON DUPLICATE KEY doesn't do anything -- it just serves to avoid returning an error if a duplicate value is inserted.
You are missing END IF keyword at the end of IF statement. And also, this SQL statement can be only used in a Routine block such as Stored Procedure or Stored Function.
Your SQL should be like this:
IF NOT EXISTS (SELECT 1 FROM Products WHERE name='Iphone1' AND manufacturer = 'appl') THEN
INSERT INTO Products(product_id, name, category, manufacturer)
VALUES (10000, 'IphoneZ', null, 'appl');
END IF;
Is it MySQL or SQL server?
I think you type wrongly : name='Iphone1' . It should be 'IphoneZ'.
You used single quotes in some places and double quotes in some places.
If it is mysql then try below query
INSERT INTO Products(product_id, name, category, manufacturer)
SELECT * FROM (select 10000,'IphoneZ', null, 'appl') AS tmp_table
WHERE NOT EXISTS (SELECT 1 FROM Products_arun WHERE name='IphoneZ'
AND manufacturer='appl') LIMIT 1;
If it is SQL server then try below query
IF NOT EXISTS (SELECT 1 FROM product WHERE name='IphoneZ' AND manufacturer='appl')
INSERT INTO product(product_id, name, category, manufacturer)
VALUES (10000, 'IphoneZ', null, 'appl');

INSERT value using SELECT in mysql

I have 2 tables: users with columns (id,username, password), and user_failed with columns (user_id, failed, time). is there any possible way i can insert into table user_failed by only using username? i try this code but it failed:
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`)
VALUES (SELECT user_id FROM users WHERE username = 'pokemon','',3)
Your SQL query is incorrect for several reasons.
The following should work if I have interpreted your query correctly.
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`)
SELECT id, '', 3 FROM users WHERE username = 'pokemon'
INSERTing into a table from a SELECT does not require VALUES ( ... ). Here is an example of how you would use VALUES ( ... ):
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`)
VALUES (1, '', 3)
Also, your sub query SELECT user_id FROM users WHERE username = 'pokemon','',3 the WHERE clause is invalid. Specifically the '',3 part which I assume is the values you wanted to insert for time and failed.
This will work....you have to add plain parentheses before and after statements.
INSERT INTO `login_attempts`(`user_id`, `time`, `failed`) VALUES ((SELECT user_id FROM users WHERE username = 'pokemon'),'',3)

Mysql database change ordering of records

Here I have data in mysql database that is reversed in order. How to change the ordering so that default ordering that is ASC is reversed.
Primary field is ID. ANd ID is auto_increment. I need to change the ID fields in reverse order for each records.
For example. Let's assume I have 2 records in table.
ID field
1 field1
2 field2
I want to have field2 to have ID 1 and field1 to have ID 2
You could do:
ALTER TABLE `table` ORDER BY `ID` DESC
but its bad database design
its against First normal form
or you can export the entire table in script file. "sql" and once the script in hand you can put in order all the (INSERT INTO) backwards. It takes a few minutes but it works
1: generate script from your base..you can use phpmyadmin or console line mysql
2. reverse the insert command
Base script Before:
INSERT INTO example (name, age) VALUES('Sandy Smith', '21' )
INSERT INTO example (name, age) VALUES('peter brad', '38' )
INSERT INTO example (name, age) VALUES('mike alves', '24' )
base script After:
INSERT INTO example (name, age) VALUES('mike alves', '24' )
INSERT INTO example (name, age) VALUES('peter brad', '38' )
INSERT INTO example (name, age) VALUES('Sandy Smith', '21' )
it is a slower but can it clean
OHTER solution ->
You can then try something like this: - Create a new table with the information from the original table by retrieving all the id and reversing with ORDER BY!
CREATE TABLE newtable SELECT ID, row1, row2, row3, row4, row5 FROM oldtable ORDER BY id DESC ;
And then delete the old table It should work without problem