Insert data in mysql from multiple sources - mysql

I want to insert values from different sources. For example
insert into a (('id','name','add'),'college')
select from b where id = 1,'abc'
Here there is no timestamp field in table b

I would rewrite your query as follows:
insert into a (id, name, add, college)
select id, name, add, 'abc' from b where id = 1
With this query, the 4th column in a will be assigned a constant value of 'abc'.

Related

Update column value after insert into table using trigger

I need to update ID column in table after Insert.
ID is varchar and auto-increment. I need to update the value of ID .
Eg.
when we insert a column and the ID value is supposed to be 10, so after the trigger is executed ID value should be updated to 10F.
Based on your comments, you do not need to modify the ID column permanently, which you should never do anyway, but only during certain queries.
To use the ID to distinguish which table a row came from in a join s also a bag idea - IDs should not have any information “encoded” in them: They should be meaningless unique values.
If your design is broken and you absolutely must modify the ID:
select concat(id, 'F') as ID, other_columns from tableA
union all
select ID, other_columns from tableB
or better, introduce an extra column that indicates which table each row came from:
select *, 'F' as origin from tableA
union all
select *, '' as origin from tableB

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.

mysql inserting data to table, duplicate

i got 2 tables that i want to combine its data.
the id is my key field (incremental and distinct).
table 1 and table 2 field description for example:
id - name - value
i want to insert all of table 2 data into table 1, they have different data but in some rows the same id.
so when i try to:
INSERT INTO ladatabase.table2 SELECT * from ladatabase.table1;
i get duplicate entry error.
please help me to suggest how can i solve it and combine the data with different id values for the new data.
Alex.
here are 2 ways:
first you can use if the id AUTO INCREMENT
INSERT INTO ladatabase.table2
SELECT '', name, value from ladatabase.table1;
or you find the first free ID and add it
INSERT INTO ladatabase.table2
SELECT id+555 , name, value from ladatabase.table1;
No insert all fields (exclude 'id'-field from SELECT):
INSERT INTO ladatabase.table2 SELECT name,value from ladatabase.table1;
You need to copy all but the ID column, and it will assign new IDs. You need to list all the other columns explicitly. Leaving out the ID column will cause it to get its default value, which are sequential IDs for an auto_increment field.
INSERT INTO table2 (col2, col3, col4, ...)
SELECT col2, col3, col4, ...
FROM table1

How should i write query in MySQL for getting values from one table to another

I want some field datas to get inserted from a table(called profile) to another table(services) along with those field datas in the table(to which the values want to be inserted,i.e. services).But I am failing. I am new to MySQL and database queries i need ur help.
$qur = mysql_query("INSERT INTO services(FirstName,LastName,DOB,Mobile,email,CountryCode,Address,State,City,Country,PinCode,altmobnumber,PanCard,AdharCard,ServiceOffering,Fee,FeeDuration,FeeExtraHour,negotiable) select profile.FirstName,profile.LastName,profile.DOB,profile.Mobile,profile.email,profile.CountryCode,profile.Address,profile.State,profile.City,profile.Country,profile.PinCode from profile where id = '$id'")or die(mysql_error());
You need to specify value for every column in your VALUES list. If you do not have any value from the SELECT statement, either use a NULL value for respective field or simply remove it from the VALUES list.
e.g. if you want to insert three values in tbl1 but tbl2 contains only 2 fields, use a NULL value for the third field
INSERT INTO tbl1 (a, b, c) SELECT tbl2.a, tbl2.b, NULL FROM tbl2
or if you have a hard coded value,
INSERT INTO tbl1 (a, b, c) SELECT tbl2.a, tbl2.b, some_value FROM tbl2
or simply use only two fields
INSERT INTO tbl1 (a, b) SELECT tbl2.a, tbl2.b FROM tbl2
Now, the VALUES part of your query has 19 fields but your SELECT statement provides only 11 fields. Either add NULL for the last 8 fields for successful execution or remove the last 8 fields from VALUES list

can this work? insert into with select...i have my own value and want to take some data from other table

can this work? insert into with select...i have my own value and want to take some data from other table
well, i haven't try this, still thinking about this
insert into table1(number, name, inventory)
| |
--my own values --from table2 --> row 1
--my own values --from table2 --> row 2
and i want a result like this
table1
number | name | inventory
Z001 | desk | 50
Z002 | chair | 100
can you help me?
Absolutely. Insert select:
insert into table1 (number, name, inventory)
select number, name, inventory from table2
If you're talking about restricting to literally rows 1 and 2:
insert into table1 (number, name, inventory)
select number, name, inventory from table2
where number = 'Z001' or number = 'Z002'
You cannot do this in one query. to insert your own values into a table you would use the following syntax:
INSERT INTO table1(number) VALUES (myValue)
And to insert values from another table you would use a different syntax:
INSERT INTO table1(name, inventory)
SELECT row1, row2 FROM table2
So you see, you cannot mix the two. I suggest doing the second INSERT first for name and inventory, than using UPDATE to insert the number.
First:
INSERT INTO table1(name, inventory)
SELECT row1, row2 FROM table2
than:
UPDATE table1
SET number = myValue
if myValue is a set of values, and not just one value, use a loop that repeats this update with an added WHERE, and myValue as a variable that changes to the next value in your set. Since I don't know on what you're using SQL, I can't tell you how to do the loop. Good luck!