sql insert to table realted to another table and constant values - mysql

I have 2 SQL tables, table1 and table2.
table1 has two columns and I want to insert values to these columns.
one of the columns should get a static value and the other column should get a value that is a result of a query from table2.
If I wanted to insert the static data separately I would do:
INSERT INTO table1(login_id)
VALUES ('1234');
and if I wanted to insert the dynamic value separately I would do:
INSERT INTO table1(user_uuid)
SELECT users_uuid FROM table2 where first_name like 'ortal';
How can insert both values to table1 in one action?
If I try the first query I get:
11:20:45 INSERT INTO table1(login_id ,user_uuid) VALUES ('1234') Error Code: 1136. Column count doesn't match value count at row 1 0.000 sec
INSERT INTO `users`.`table1` (`login_id`) VALUES ('1234');
ERROR 1364: 1364: Field 'user_uuid' doesn't have a default value

You can add the constants to your select list and treat them a columns:
INSERT INTO table1(user_uuid, login_id)
SELECT users_uuid, '1234' FROM table2 WHERE first_name LIKE 'ortal';

Related

MYSQL Get id from Table1 and Insert to Table2 while INSERTING Values (USER Form)

I have 2 tables:
Table1
u_id email
1 user1#y.com
2 user2#h.com
and
Table2
id u_id values
had to insert values to table2 with u_id when user enters from form
Example:I need to add the value table1 u_id while that user is logged in into table2 that is id increments in table and u_id from table1 and values as user enters from table2
i tried something this query however not successful
insert into table2 (u_id,values)
values ((select u_id from table1), 'values')
The problem with your query is that the subquery returns multiple rows (one value per row in table1), while the database expects a scalar value.
I would expect that you know the email of the current user, and want to recover the corresponding u_id to insert a new row in table2, along with a literal value. If so, I would recommend the insert ... select syntax:
insert into table2 (u_id, val)
select u_id, 'foo' from table1 where email = 'user1#y.com'
Side note: values is a SQL keyword, hence not a good choice for a column name (you would need to quote this identifier every time you use it). I renamed this column to val in the query.

my sql using insert and select together

Im trying to use insert and select in one query.Both the tables have same number of columns except for one column in the table where data is to be inserted
table2 is the mirror image of table1 except for a column called comments;
insert into table2 select * from table1 where city='XYZ' and name = 'STY'
since the no of columns arent equal i get the following error:
Column count doesn't match value count at row 1
INSERT INTO table2 (Coln1,Coln2,Coln3,....) SELECT * FROM table1 WHERE `city`='XYZ' AND `name`='STY';
For the extra column in the table2, NULL is set by default.

Mysql Insert value from another table

I have two tables : table1(t1_id, t1_name),
table2(t2_id, t1_id).
now if i try to insert value into table2 like this:
INSERT INTO table2(t2_id, t1_id) values(110202,(SELECT t1_id FROM table1));
This create an error : "subquery returns more than 1 row", which means it can insert only one row. But i want to insert all cartesian product.
May be you are looking for this
INSERT INTO table2(t2_id, t1_id)
SELECT 110202,t1_id FROM table1

Copy from one column to another (different tables same database) mysql

Hi I would like to copy entire contents from column Item under table IName to column Name under table Item both belonging to the same database.
I am giving the following query but it throws the error saying that the subquery returned more than one records. (There are around 600 records)
Insert into Item set name = (Select Item from IName)
Thanks
INSERT INTO Item (Name)
SELECT Item
FROM IName
When you want to insert into a single-column* table, INSERT works either with:
INSERT INTO table (column)
VALUES (value1),(value2), ... (valueN) ;
or with:
INSERT INTO table (column)
SELECT a_column
FROM a_table
--- optional (multiple) JOINs
--- and WHERE
--- and GROUP BY
--- any complex SELECT query
(OK, the above can work with a multiple-column table, too, as long as all the other - not explicitely stated in the INSERT statement - columns have been defined with a DEFAULT value or with AUTO_INCREMENT.)
The INSERT ... SET syntax is valid in MySQL only and can be used only when you want to insert one row exactly:
INSERT INTO table
SET column = value1 ;
is equivalent to:
INSERT INTO table (column)
VALUES (value1) ;
INSERT INTO Item (name)
SELECT Item FROM IName
Link
INSERT INTO table_one (column1) SELECT column2 FROM table_two
See MySQL Ref

Insert a row for each result of other query in SQL

If I have two tables with following columns:
Table1: [id,value]
Table2: [id,comment]
where id and value are numeric and comment is a string.
I need to retrieve all the ids with value>50 in the Table1.
And make an
INSERT IGNORE INTO table2 VALUES (id,"higher than 50");
for each one of those ids. How can I make it in MySQL? Thanks
INSERT IGNORE INTO table2
SELECT id,"higher than 50"
FROM Table1 where value > 50;