Sectionname gradelevel and teachersid
How can use join table ? How can i link into the other table
Select a.sectionname AS sec b.gradelevel AS level c. Teachersid AS teacher
There are a few different ways that you can do this. However, in order to do this, you must have a foreign key in the table to be joined. You may be looking for something like the following example, but I cannot give you an exact answer without knowing the fields of the tables.
SELECT A.SECTIONNAME, B.GRADELEVEL, C.TEACHERSAID
FROM SECTIONNAME, GRADELEVEL, TEACHERSAID
WHERE SECTIONNAME.ID = GRADELEVEL.ID
AND SECTIONNAME.ID = B.GRADELEVEL.ID
AND GRADELEVEL.ID = C.TEACHERSAID.ID;
SELECT TableA.*, TableB.*, TableC.*, TableD.*
FROM TableA
JOIN TableB
ON TableB.id = TableA.id
JOIN TableC
ON TableC.id = TableB.id
JOIN TableD
ON TableD.id = TableA.id
Related
I have a staging table through which I want to delete all matching records in my Customers table. In "language terms":
delete
tableA.*
from
table A,table B
where
TableA.col1=TableB.col1
&& TableA.colb=TableB.col2 /// and so forth
Some info about the tables:
There are no relationships between the tables. The only true way to match the records is to match all of the columns (I want to clear any duplicates)
There are no foreign keys inplace between the 2 tables. Staging table is imported from CSV and the data will be transformed to use within our system.
Most of the imports will be identical (with around 80% of the staging rows to be deleted from around 60k records)
I have this working in Linq2SQL but it's taking a longer due to all of the queries and as there is around 80% matching records with each query and I feel a single query should be suffice.
Is this at all possible in SQL?
You can use JOIN with DELETE
DELETE a
FROM tableA a
INNER JOIN tableB b
ON a.Col1 = b.Col1
AND a.ColB = b.ColB
... and so on
or by using EXISTS:
DELETE a
FROM tableA a
WHERE EXISTS
(
SELECT 1 FROM tableB b
WHERE a.Col1 = b.Col1
AND a.ColB = b.ColB
....
)
merge table1 t1
using (
select t2.ID
from table2 t2
) as d
on t1.ID = d.ID
when matched then delete;
I have the following tables in my database:
SageAccount
ID (bigint)
LegacyID (nvarchar)
Customer (bit)
Consignments
ID (bigint)
Customer (nvarchar)
What I want to do is have a navigation property/association in my Linq to Sql dbml from Consignment to SageAccount. The difficulty with this is that not only do we need to match SageAccount.LegacyID => Consignments.Customer but we also need to only join to sage accounts where SageAccount.Customer is TRUE. So on the Consignments end, it isn't joining onto a field but instead a static value.
Is this possible in Linq to Sql? Note this database doesn't (and unfortunately can't) have any foreign keys setup in the database.
Yes it is possible. linq have join method. You can use it ike this in your situation:
var res = from sageAccount in _context.SageAccount
join consignments in _context.Consignments
on
new
{
LegacyID = sageAccount.LegacyID,
Customer = sageAccount.Customer
}
equals
new
{
LegacyID = consignments.ID,
Customer = true
}
select new { SageAccountID = sageAccount.ID };
Note that Property name, Type and order in the anonymous objects that you're joining on must match.
You can't use OR and AND in joins - use just equals one object to other.
This will have a this kind of result in your SQL:
SELECT [t0].[ID] AS [SageAccountID]
FROM [dbo].[SageAccount] AS [t0]
INNER JOIN [dbo].[Consignments] AS [t1] ON (([t0].[LegacyID]) = [t1].[ID])
AND ([t0].[Customer] = 1)
I have 3 tables with various columns:
tableA = id(PK) & name columns
tableB = id(PK), A_ID(foreign key to tableA), name, address, etc columns
tableC = id(PK), A_ID(foreign key to tableA), name columns
I’m trying to use the following query to retrieve values from certain columns within all tables based on tableA name = ‘something’, but always returning syntax errors.
“SELECT tableA.name, tableB.name, tableB.address, tableC.name FROM
tableA, tableB, tableC JOIN tableB ON tableA.id = tableB.A_ID JOIN tableC
ON tableA.id = tableC.A_ID WHERE tableA.name = ‘something’”
You have to remove tables from from statement if you want to use join syntax
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM tableA
INNER JOIN tableB ON tableA.id = tableB.A_ID
INNER JOIN tableC ON tableA.id = tableC.A_ID
WHERE tableA.name = 'something'
I suggest you to use aliases, the code could be more readable:
SELECT A.name, B.name, B.address, C.name
FROM tableA A
INNER JOIN tableB B ON A.id = B.A_ID
INNER JOIN tableC C ON A.id = C.A_ID
WHERE A.name = 'something'
Ms Access requires you to specify the type of join: INNER; LEFT; or RIGHT. Access does not recognize just JOIN as a synonym for INNER JOIN.
A query which includes more than one join requires parentheses in the FROM clause.
I also changed your quote characters to plain " and '. The sample query included type-setting quotes. I don't know if they are present in the actual SQL, but I would avoid them.
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM
(tableA
INNER JOIN tableB
ON tableA.id = tableB.A_ID)
INNER JOIN tableC
ON tableA.id = tableC.A_ID
WHERE tableA.name = 'something'
If you have the full version of Access available, use the query designer to set up the joins. The designer knows the syntax rules which keep the db engine happy.
otherwise you could do it like:
SELECT tableA.name, tableB.name, tableB.address, tableC.name
FROM tableA, tableB, tableC
WHERE tableA.id = tableB.A_ID AND tableA.id = tableC.A_ID
AND tableA.name = ‘something
Parado's answer is correct..
Moreover you can simplify the length of the query by using aliases for the table names.. And it is good practice too..
select a.name, b.name, b.address
from tableA a
join tableB b
on a.id = b.A_ID
I have a list of SiteUsers in one table and another table has columns with different types of owners (ID) for the record. For example, the SiteUserID in the SiteUsers table will be used for the SalesRepID, the StaffingManagerID, and RecruiterID in the Fill table. Of course, the SiteUserID is different for each of the values in the Fill table.
I'd like to return the name of the SiteUser for each ID column in the Fill Table.
How do I properly construct a JOIN statement to do this?
I'm guessing this is done through INNER JOIN, but I'm not sure.
My current select statement already has an INNER JOIN as I'm pulling the name of the FillType from another table. I'm using this in an asp.net application.
I'm not sure if this is even possible. Any help is appreciated.
Since each of the IDs in the Fills table allows null, you probably want to LEFT JOIN to the SiteUsers table like so:
SELECT f.FillID, s1.SiteUserLastName 'SalesRep', s2.SiteUserLastName 'StaffingManager', s3.SiteUserLastName 'Recruiter'
FROM Fills f
LEFT JOIN SiteUsers s1 on f.SalesRepID = s1.SiteUserID
LEFT JOIN SiteUsers s2 on f.StaffingManagerID = s2.SiteUserID
LEFT JOIN SiteUsers s3 on f.RecruiterID = s3.SiteUserID
You can always UNPIVOT the results like so:
SELECT
DISTINCT
unpvt.FillID
,unpvt.RepID
,unpvt.RepType
,s.SiteUserFirstName
,s.SiteUserLastName
FROM
(SELECT
FillID
,SalesRepID
,StaffingManagerID
,RecruiterID
FROM Fills
) f
UNPIVOT
(RepID FOR RepType IN
(SalesRepID, StaffingManagerID,RecruiterID)
) AS unpvt
JOIN SiteUsers AS s on unpvt.RepID = s.SiteUserID`
Obviously you can play with exact output (such as substituting the RepType for a different value with a CASE statement or whatnot.
My question is: why the piss-poor design? Instead of having three IDs in the Fills table, you should have a junction table between SiteUsers and Fills to allow many-to-many relationships. IF it were designed with a junction table, you'd never have had to ask this question.
You will have to join the Fill table with the SiteUsers table multiple times, one for each xxxID column in the Fills for which you want the SiteUser name and combine the results using an union as below:
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.SalesRepId = a.SiteUserId
UNION
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.StaffingManagerId = a.SiteUserId
UNION
select a.SiteUserId, a.SiteUserFirstName, a.SiteUserLastName
from dbo.SiteUsers a
inner join dbo.Fills b on b.RecruiterId = a.SiteUserId
I'm trying to make a query that grabs a single row from an SQL database and updates it.
TableA
AId
AValue
TableB
BId
AId
BValue
Ok, so TableA and TableB are linked by AId. I want to select a row in TableB based on AValue using a join. The following query is what I have and only grabs a value from TableB based on AId, I just don't know how to grab a row from TableB using AValue. I know you would need to use a join, but I'm not sure how to accomplish that.
var row = DbObject.TableB.Single(x => x.AId == 1)
row.BValue = 1;
DbObject.SubmitChanges();
Below is a LINQ query to do what you are asking.
var row = (from b in TableB
join a in TableA on a.AId equals b.AId
where a.AValue = yourValue).Single();