I have three mysql table from same database Db1.
Three tables have following columns.
Table 1:
Name
City
Branch
Table 2:
Age
Address
Country
Table 3:
No columns.
I want to copy Table1.Name and Table2.Age to Table 3. How can I do it?
This makes little sense if table1 and table2 can not be joined and table 3 doesn't have the 2 columns. If you can join:
insert into table3 (name, age)
select table1.name, table2.age
from table1 join table2 on (table1.columnToLinkFromTable1 = table2.columnToLinkFromTable2)
You could also do it like this, but of course it doesn't make much sense:
insert into table3 (name, age)
select table1.name, table2.age
from table1, table2
Related
insert INTO table1 (price,service_id,country_id)VALUES(2.00,(SELECT id from table2), (SELECT id from table3))
It shows error.
Table 1-
id, price, service_id, country_id
Table 2-
id, service_name
Table 3-
id, country_name
When inserting in mysql using select query, you don't need the values() anymore.
Try this:
insert INTO table1 (price,service_id,country_id) Select t2.ID, t3.ID from table_2 t2, table_3 t3
Here is one example.
I have two tables, Table1 and Table2.
Table1 has the columns "ID","Date" and Table2 has the columns "ID", "Cost".
Now, the way I want to merge these two tables into a new table Table3 with columns "ID", "Date", "Cost" is that the Cost and Date are in the same row which have the same ID in both Table2 and Table1 respectively.
In short, I want to glue the two tables with respect to a column, in this case "ID".
I've looked into statements like INSERT INTO TABLE but I haven't been able to get it to work.
You can perform an insert-select on the result of the join between the two source tables:
CREATE TABLE table3 AS
SELECT table1.id AS id, date, cost
FROM table1
JOIN table2 ON table1.id = table2.id
please use below SQL to merge
insert into table3 (id, date1, cost)
select a.id, b.date1, a.cost from table1 a, table2 b where a.id=b.id;
I have:
table1:
Id | Name
table2:
Id | Amount
I want create a new table based on the common Id. So if a record from table1 and table2 have matching id, then:
table3
Id | name | Amount
Sorry if this has been asked before. I'm new to this and just want to get this done
Why wouldn't you do this with a simple select statement?
SELECT a.id, a.Name, b.Amount
FROM table1 a, table2 b
WHERE a.id = b.id
Try using a JOIN
SELECT table1.id, table1.name, table2.amount
FROM table1
LEFT JOIN table2
ON table1.id=table2.id;
I didn't test this but I think it should work.
http://www.w3schools.com/sql/sql_join_left.asp
Like this :
CREATE TABLE tavle_xxx(
id xxx,
name xxx,
amount xxx
);
INSERT IGNORE INTO tavle_xxx
SELECT t1.id, t1.name, t2.Amount
FROM table1 t1, table2 t2
WHERE t1.id=t2.id;
update:
I'd like to merge the tables based on the common column "id" and NOT create a 3rd table. Just add the "name column" and populate it where the id's are =
so i have:
table1:
Id | Name
table2:
Id | Amount
I would like the result to be
table2
Id | Amount | Name
If you want to merge the Name column of table1 in table2, using the common column Id, you can do this by running:
ALTER TABLE table1 ADD Name <dataTypeOfName>;
UPDATE table1, table2 SET table2.Name = table1.Name WHERE table1.Id = table2.Id;
This will create a new column called Name in table2 and then fill it with the values from table1, after performing a join based on the column Id.
I have four tables having same structure. Say they have a column name in them. I need to check if values in column name of table1 is present in column name of table2.
If present, I need to insert this name in column name of table3, else in column name of table4.
Assuming INSERT and Postgres ...
I would use a data-modifying CTE (Postgrs 9.1+) with one SELECT and two INSERT:
WITH cte AS (
SELECT t1.name, t2.name IS NULL AS t2_missing
FROM table1 t1
LEFT JOIN table2 USING (name)
)
, ins AS (
INSERT INTO table3 (name)
SELECT name FROM cte WHERE NOT t2_missing
)
INSERT INTO table4 (name)
SELECT name FROM cte WHERE t2_missing;
The same is not possible in MySQL (no CTEs, not to mention writable CTEs).
I have two table in database.
Table1 -> Name
Table2 -> Name
What will be query to get all the "Name" from Table1 and Table2 into single Column.
This query returns the value from the Name column from Table1 and the Name column from Table2, concatenated together into a single resultset.
SELECT t1.Name FROM Table1 t1
UNION ALL
SELECT t2.Name FROM Table2 t2
(This was my understanding of what you were looking for.)
If you want just a "distinct" list of Name values (exclude duplicate occurrences of the same value), then remove the ALL keyword.
If I correctly understood
http://dev.mysql.com/doc/refman/5.0/en/union.html
Select name from table1
union
Select name from table2
You can select data from two tables like this.
SELECT CONCAT(table1.name,table2.name) as Name FROM table1,table2;
and in case table1.name is A and table2.name is b you get
Name = AB
SELECT Name FROM Table1 NATURAL LEFT JOIN Table2 AS t2.
This will give you a list of only non-duplicate names from Table1 and Table2.