I have two tables. I need to update the records of the 1st table performing a join with the 2nd table. But in the 2nd table I also have to put a self join.
UPDATE [T2]
SET T2.NAME = T1.NAME
FROM [TABLE2] T2
JOIN [TABLE1] T1 ON T2.ID = T1.ID
JOIN [TABLE1] T3 ON T1.RECORDTYPE = T3. RECORDTYPE
The purpose is to perform a self-join and update the other table.
Is this what you mean?
UPDATE T2
SET T2.NAME = T1.NAME
FROM T2
JOIN T1
ON T2.ID = T1.ID
AND T2.RECORDTYPE = T1.RECORDTYPE;
Related
I want to write an SP that I will use in my c# program. I have three tables: Table1 (id pk, name, ...), Table2 (id PK, name, ...) and Table3((idT1,idT2) PK FK). So, Table3 models the n:n relationship between Table1 and Table2.
I want to retrieve all the Table2.name(s) related to a single Table1.name.
I have already tried to write a query with two inner join
CREATE PROCEDURE slct
#name nvarchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT Table2.name from Table2
join Table3 on Table2.id = Table3.idT2 join Table1 on Table1.id = Table3.idT1 where Table1.name = '%'+#name+'%'
END
GO
Table1
|1|abc|
|2|def|
Table2
|1|xyz|
|2|mno|
Table3
|1|1|
|1|2|
|2|2|
As result, I see more records than are needed. I expected a list of Table2.name related to a single Table1.name (specified by #name parameter)
sorry for my english.
select Table2.Names from Table2
inner join
(select Table3.idT1 as SubQueryID1, Table3.idT2 as SubQueryID2
from Table3 inner join Table1 on Table1.id = Table3.idT1)
on
Table2.Id = SubQueryID2
SELECT
Table2.name
FROM Table2
INNER JOIN Table3 ON Table2.id = Table3.idT2
CROSS APPLY(
SELECT TOP 1 * FROM Table1 WHERE Table1.id = Table3.idT1
) t
WHERE
Table1.name = '%'+#name+'%'
I want to retrieve all the Table2.name(s) related to a single Table1.name.
select t2.name
from table2 t2 join
table3 t3
on t3.idT2 = t2.id
group by t2.name
having count(*) = 1;
This returns names in t2 that have only one row in table3. This makes two assumptions (both of which are reasonable for this data structure):
Names are unique in table1 and table2.
Pairs are unique in table3.
I want to retrieve all the Table2.name(s) related to a single Table1.name.
Your query actually has the right join logic. You have to clarify the scope of the where clause.
Table1.Name contains #name: you need the like operator instead of the equal operator.
Table1.Name strictly equals to #name: you have to remove the wildcards.
See below two examples:
select distinct T2.Name
from Table1 as T1
inner join Table3 as T3 on T3.IDT1 = T1.ID
inner join Table2 as T2 on T2.ID = T3.IDT2
where T1.Name like '%'+#name+'%'
or
select distinct T2.Name
from Table1 as T1
inner join Table3 as T3 on T3.IDT1 = T1.ID
inner join Table2 as T2 on T2.ID = T3.IDT2
where T1.Name = #name
How can I convert this outer left join to LINQ?
SELECT * from table1 t1
left join table2 t2 on t1.code = t2.code AND t2.id ='xxxxx'
WHERE t1.someId = 2
I'm trying something like this:
from t1 in db.table1
join t2 in db.table2 on t1.Code equals t2.Code
into oj
from sub in oj.DefaultIfEmpty()
where t1.someId == 2
where (sub.id == 'xxxx')
but not all rows from left table are being returned. The where clauses i think are applied after the join.
var res=(from t1 in table1.Where(x=>x.someId==2)
join t2 in table2.Where(y=>y.id==xxxx)
on t1.code = t2.code
into r
from t3 in r.DefaultIfEmpty()
select new {t1,t3}).ToList();
Need to retrieve result set of both tables but i am getting null value in it
var Items = from t1 in dbModal.Table1
join t2 in dbModal.Table2 on t1.id equals t2.Id
select new
{ t1, t2.column };
need to map above query like this
Select t1.*,t2.Column from T1
join T2 on T1.id=T2.Id
I have few large tables and I need to join them. In SQL it looks like:
select * from dbo.Table1 t1
join dbo.Table1 t1Parent on t1.ParentId = t1Parent.Id
join dbo.MappingT1T3 t2 on t1Parent.Id = t2.ExternalId and t2.[Type] = 1
join dbo.Table3 t3 on t2.ForeignId = t3 .Id
where t1.[Type] = 3
Tried to convert this query to a such LINQ:
from t1 in dbo.Table1
join t1Parent in dbo.Table1 on t1.ParentId equals t1Parent.Id
join t2 in dbo.MappingT1T3 on new { Id = t1Parent.Id, [Type] = (int)1 } equals new { Id = t2.ExternalId, [Type] = (int)t2.[Type] }
join t3 in dbo.Table3 on t2.ForeignId equals t3.Id
where t1.[Type] == 3;
But seems execution plan differs a lot. Profile says that it tries to load all tables without conditions..
Try putting the constant to a seperate condition...
from t1 in dbo.Table1
where t1.[Type] == 3 // <--- PUT THIS ONE HIGHER
join t1Parent in dbo.Table1 on t1.ParentId equals t1Parent.Id
join t2 in dbo.MappingT1T3 on t1Parent.Id equals Id = t2.ExternalId
where (int)t2.[Type] == 1 // <--- SEPARATE CONDITION
join t3 in dbo.Table3 on t2.ForeignId equals t3.Id;
in my database there is a table has 3 FK to other 3 tables i want to select the data in this table + data in the other 3 tables when the FK value = the PK values in the other tables
SELECT *
FROM t1
JOIN t2
ON t2.id = t1.t2_id
JOIN t3
ON t3.id = t1.t3_id
JOIN t4
ON t4.id = t1.t4_id
SELECT t1.Field1, t2.Field2, t3.Field3, t4.Field4
FROM Table t1
INNER JOIN Table2 t2 ON t1.ID = t2.ID
INNER JOIN Table3 t3 ON t1.ID = t3.ID
INNER JOIN Table4 t4 ON t1.ID = t4.ID
If there aren't always matching rows in Tables 2 - 4, and you want those records return still then change the INNER JOINs to LEFT JOIN
SELECT * FROM ParentTable as pt
LEFT JOIN table1 as t1
ON pt.t1_ID = t1.ID
LEFT JOIN table2 as t2
ON pt.t2_ID= t2.ID
LEFT JOIN table3 as t3
ON pt.t3_ID= t3.ID
This will return a record even if there are not matches in table1,2 or 3. #Ada and #Q's will exclude those records.
SELECT * FROM TBL1 T1
RIGHT JOIN TBL2 T2
ON T1.id = T2.id
RGIHT JOIN TBL3 T3
ON T3.id = T2.id
This will return all the records from Right table and matching records from Left table.