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

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

Related

MySql filling the rest of the data after I have selected a distinct value

ive done this to pull the distinct data
INSERT INTO `goac`.`customer`
( CUST_ID)
SELECT DISTINCT CUST_ID
FROM ods_customer
but I would like to bring the rest of the data with it and have it fill out the table.
I am pulling data from 2 tables for this one.
According to your info above. I guess...
Try to add more column in insert statement
INSERT INTO table_name VALUES (value1, value2, value3...);
Maybe like
INSERT INTO `goac`.`customer` ( CUST_ID, CUST_NM, CUST_INCOME_AM, CUST_STREET_AD...) SELECT DISTINCT CUST_ID FROM ods_customer

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

Operand should contain 1 column(s) while insert in 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.

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)

How to use procedure variable in select and insert query in MySQL?

I have two variables
1. inserted_user_id,
2. inserted_address_id
in my MySQL procedure.
I need to use them in insert queries and select queries. Im trying something like
insert into user_new(name, company, email, customer_id) select name,
company, email, inserted_user_id from user_old;
insert into user_address_map(address_id, user_id) select
inserted_user_id,inserted_address_id ;
There two statements are not working. How to use procedure variable's value in the above sql statements?
First make a select to get all the info: (you need to declare these variables first)
SELECT name,
company,
email,
INTO varname, varcompany, varemail
FROM user_old
then use it into insert
INSERT INTO user_new
(name,
company,
email,
customer_id)
VALUES (varname,
varcompany,
varemail,
inserted_user_id);
edit:
First insert the Selected values and get the id of the inserted row
INSERT INTO user_new
(name,
company,
email)
SELECT name,
company,
email
FROM user_old
SET out_param = last_insert_id();
Then update this row with your param
UPDATE user_new
SET customer_id = inserted_user_id
WHERE id = out_param;