Im trying to create a view where specific Columns from 2 tables to be combined.
My Tables
Table1(Column1,Column2,Column3)
Table2(Column4,Column5,Column6)
Expected output
View1(Column2,Column3,Column6)
What query can i use to achieve that output?
CREATE VIEW [dbo].[View]
AS
SELECT a.Column2,a.Column3,b.Column6
FROM Table1 AS a
INNER JOIN Table2 AS b ON A.ID = B.ID -- your logic
If you have and id to join tables:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
join table2 t2 on t1.id = t2.id
If you don't have and id and want to get mixes columns without relationships:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
cross join table2 t2 on t1.id = t2.id
As Fourat and jarhl commented above, you need to explicitly define what columns are used to JOIN both tables. So using the below code change the ID column to the column that relates these two tables, and if it's more than one column the list them all separated by AND.
CREATE VIEW dbo.YourViewName
AS
SELECT t1.Column2, t1.Column3, t2.Column6
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
GO
Related
I have two MySql Databases.
One has:
___id___|___name___|__date____|
Second database has
___id___|___tag___|
One common thing in this tables - id. It's same in both tables. How can i append column "tag" to first mysql database?
If you just want a view of the columns in the first table along with a possible matching tag, then use a query:
SELECT
t1.id, t1.name, t1.date, t2.tag
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id;
If you instead want to actually add a new tag column to the first table, then add that column and then do an update:
ALTER TABLE table1 ADD COLUMN tag VARCHAR(55);
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.tag = t2.tag;
SELECT Table1.Filter, Table1.Condition, Combined.Data FROM Table1
LEFT JOIN
(SELECT Key, Data FROM IF(Table1.Filter, Table2, Table3))) AS Combined
ON Table1.Condition = Combined.Key
I want to create a MySQL View that shows all columns of Table1, and a column from either Table2 or Table3 depending on the field on Table1.Filter.
One simple solution is to LEFT JOIN both Table2 and Table3, with NULL on the column that is not applicable. Is there a way to avoid creating 2 columns?
I cannot UNION Table2 and Table3 as they might contain the same Key.
The following should do what you want:
SELECT t1.Filter, t1.Condition,
COALESCE(t2.Data, t3.Data) as Data
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Filter AND t2.Key = t1.Condition LEFT JOIN
Table3 t3
ON (NOT t1.Filter) AND t3.key = t1.condition;
You cannot have conditionals choosing tables in the FROM. But, you can have conditions in the ON conditions.
I have two tables,first table T1 contains id and name and the name contains 5 values.And the second table T2 contains id and amonut_paid and the amount paid column contains 3 values.I want to display the all names from the table T1 and the amount_paid in sql
Do a LEFT JOIN and then use IFNULL for replacing the NULL with 0
SELECT T1.name,IFNULL(T2.amount_paid,0)
FROM T1
LEFT JOIN T2 ON T1.id=T2.id
Note: Instead of IFNULL you can also use COALESCE as strawberry suggested.
Use left join here, Because your t1 table contains 5 names and you want to display all name from t1 table. And your t2 table contains only three entry(amount_paid).
Select t1.name, t2.amount_paid
from t1
left join t2 on t1.id = t2.id
Use an LEFT JOIN on both these tables T1 and T2, so that it shows only the rows where a NAME against AMOUNT_PAID exists (INNER JOIN provides the rows available in both these tables)
SELECT T1.name, T2.amount_paid
FROM T1
LEFT JOIN T2 ON T1.id = T2.id
--RETURNS 3 rows as per your example
Use an OUTER JOIN on both these tables T1 and T2, so that it shows all the rows irrespective of the values being present in these tables.
RETURNS 5 rows as per your example
I have three tables, two certainly with data values, one the values can be present or not.
This is an example schema
Table 1
id, username
Table 2
id, street
Table 3
id, phone_number (this can be not present)
please help me with query
SELECT t1.ID, t1.Username, t2.street, t3.phone_number
FROM Table1 as t1
INNER JOIN Table2 as t2 on t1.id = t2.id
LEFT OUTER JOIN Table3 as t3 on t1.id = t3.id
The following will get me values from a reference table t2 I want to insert to or to update existing tuple with in table t1:
SELECT
id, col1
FROM
t2
LEFT OUTER JOIN
t1
ON
t2.id=t1.id
If a tuple with id already exist in t1, it should be updated with the value selected from t2. If a tuple with id does not exist in t1, (id, col1) should be inserted with other columns set to default values.
How to do this efficiently?
use this two querys:
This will join and filter, giving you the values that exists in both tables, so you just do the update
Update t1 set t1.col1 = t2.col1
from t1 inner join t2 on t1.id = t2.id
This will join and filter, giving you the values that are in t2, but not in t1, so you just do the insert.
insert into t1 select t2.id, t2.col1
from t2 left outer join t1 on t2.id = t1.id where t1.id IS NULL
UPDATE:
as I can see from here MySQL uses another sintax for this. So you query may work with this instead of the query above:
UPDATE t1 temp1
INNER JOIN t2 temp2
ON temp1.id= temp2.id
SET temp1.col1= temp2.col1
but the concepts are the same (just a different syntax)
You won't need the Where, because the INNER JOIN will only use fields that match/join.