Multiple joins in SQL statement - c#

To clear things up, I have two tables, employees and jobs.
In employees I have columns firstname, lastname, employees id.
In jobs I have columns
job_id, job_name, job_description, job_opener, job_responsible_person
where the last two are foreign keys which refer to the employees table.
This is my SQL statement
String cmd = "SELECT je.id, firstname, lastname, job_name FROM jat.employees je" +
" INNER JOIN jat.jobs jj ON je.id = jj.job_opener ";
And I fill data table with it.
What I need is adding two more columns, like second first name and second last name, in same table, but which will depend on job_responsible_p, so ON clause would be
ON je.id = jj.job_responsible_p
Is that possible in one query?

Yes, you can join several table at once:
SELECT je.id, je.firstname, je.lastname, jj.job_name, jr.firstname, jr.lastname
FROM jat.employees AS je INNER JOIN jat.jobs AS jj INNER JOIN jat.employees AS jr
ON je.id=jj.job_opener AND jr.id=jj.job_responsible_person;

Yes it is since you can use conditional operator like AND OR with JOIN ON condition.
INNER JOIN jat.jobs jj
ON je.id=jj.job_opener
AND je.id=jj.job_responsible_p

Related

how to join 3 table in mysql workbench

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

MS Access Database SQL Query

I have 3 Tables called Invoice, Customer and Company. I want to merge this 3 tables into single using Query. In Invoice Table contain Customer Id and Company Id. How to Join 3 tables ?
I tried Invoice and Customer Table working fine with this query. But I dont have idea to add 3rd table with this.
SELECT RPT_Invoice_Less.InvoiceNumber, RPT_Invoice_Less.Terms,
RPT_Invoice_Less.Invoicedate, RPT_Invoice_Less.OurQuote,
RPT_Invoice_Less.SalesPerson, RPT_Customer.CustomerName,
RPT_Customer.CustomerId, RPT_Customer.ContactPerson,
RPT_Customer.BillingAddress, RPT_Customer.DeliveryAddress,
RPT_Invoice_Less.OrderNumber, RPT_Invoice_Less.ShippingBy,
RPT_Invoice_Less.ShipReferenceNo, RPT_Invoice_Less.Notes,
RPT_Invoice_Less.Price, RPT_Invoice_Less.Discount,
RPT_Invoice_Less.Shipping, RPT_Invoice_Less.Tax,
RPT_Invoice_Less.GrandTotal, RPT_Invoice_Less.Company
FROM RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId;
this code working fine for 2 tables
SELECT RPT_Invoice_Less.InvoiceNumber, RPT_Invoice_Less.Terms, RPT_Invoice_Less.Invoicedate, RPT_Invoice_Less.OurQuote, RPT_Invoice_Less.SalesPerson, RPT_Customer.CustomerName, RPT_Customer.CustomerId, RPT_Customer.ContactPerson, RPT_Customer.BillingAddress, RPT_Customer.DeliveryAddress, RPT_Invoice_Less.OrderNumber, RPT_Invoice_Less.ShippingBy, RPT_Invoice_Less.ShipReferenceNo, RPT_Invoice_Less.Notes, RPT_Invoice_Less.Price, RPT_Invoice_Less.Discount, RPT_Invoice_Less.Shipping, RPT_Invoice_Less.Tax, RPT_Invoice_Less.GrandTotal, RPT_OrionSystem.Company, RPT_OrionSystem.CompanyId
FROM RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId
INNER JOIN RPT_OrionSystem
ON RPT_Invoice_Less.CompanyId = RPT_OrionSystem.CompanyId;
This code showing syntax error.
Help me to add 3rd Company table to this.
Supposing that you have a CompanyID field (or something like that) in the RPT_Customer table or in the RPT_Invoice_Less, it is just a matter to add another INNER JOIN
....
FROM ((RPT_Invoice_Less
INNER JOIN RPT_Customer
ON RPT_Invoice_Less.CustomerId = RPT_Customer.CustomerId)
INNER JOIN RPT_OrionSystem
ON RPT_Invoice_Less.CompanyID = RPT_OrionSystem.CompanyID)

Syntax error (missing operator) in query with parenthesized FROM clause

I am working with asp.NET and C# with an access database to create a web application.
My current issue is with an SQL statement. In the project i have already successfully used a string value to build and store a query. Because it is stored in a string it is a bit ugly to look at. The problem is with this last query i am writing. It has 2 inner joins in it and i am unsure what exactly is wrong.
The error i get is a syntax error (missing operator) and it then lists everything inside the parenthesis. Here is the query:
SELECT Employee.[First Name], Employee.[Last Name], Employee.[Email],
Departments.[Department]
FROM (
Employee INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
INNER JOIN Departments ON Departments.[Department Number] = '2'
)
WHERE Departments.[Campus]='Clarion';
It is very ugly like this, i know.. Im hoping that since this is a syntax error it wont be too hard.
This query is designed to return the name, email, and department of an employee. The 2 is given with c# code and is determined earlier in the code, but it stands for a certain department. The empDept table goes between the Departments table and the employee table so an employee can be in more than one department.
Any help is greatly appreciated. Thanks
Just remove the parenthesis. They force the database to try to treat the entire expression as a single table, which isn't right.
Also, based on your description, I'd write the query to match the department in the join with what's in the EmpDept table, and then use the WHERE clause to filter down to dept '2'. Right now you filter the Department table down to department '2', but leave it unconditionally related to the rest of the query. This means you're pulling in Employee records from any department.
Finally, I consider it a good practice to get in the habit of using table aliases. Not only does it make your code shorter, but more advanced queries will often pull from more than one instance of the same table, and the aliases make it explicit which instance of the table you mean.
SELECT e.[First Name], e.[Last Name], e.[Email], d.[Department]
FROM Employee e
INNER JOIN EmpDept ed ON e.[EmpUserName] = ed.[EmpUserName]
INNER JOIN Departments d ON d.[Department Number] = ed.[Deptartment Number]
WHERE d.[Campus]='Clarion' AND d.[Department Number] = '2';
Try this
SELECT Employee.[First Name], Employee.[Last Name],
Employee.[Email], Departments.[Department]
FROM Employee
INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
INNER JOIN Departments ON EmpDept.[DepartmentId] = Departments.[Id]
WHERE Departments.[Campus]='Clarion'
AND Departments.[Department Number] = '2'
You'll need an ID on the EmpDept table that matches the Departments table.
SELECT Employee.[First Name], Employee.[Last Name], Employee.[Email], Departments.[Department]
FROM Employee
INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
INNER JOIN Departments ON Departments.[Department Number] = Employee.[Department Number]
WHERE Departments.[Campus]='Clarion' and Departments.[Department Number]=2
There is a small syntax error in squery, it better to join tables with column name and mention column value condition in where clause..
Access requires parentheses within the FROM clause if it includes more than one join. As a first step, try a query like this in the Access query designer.
SELECT
Employee.[First Name],
Employee.[Last Name],
Employee.[Email],
Departments.[Department]
FROM
(Employee
INNER JOIN EmpDept
ON Employee.[EmpUserName] = EmpDept.[EmpUserName])
INNER JOIN Departments
ON Departments.[Department Number] = '2'
WHERE Departments.[Campus]='Clarion';
I think I placed the parentheses correctly; you can confirm in the query designer. However, I'm puzzled by the second ON clause.
ON Departments.[Department Number] = '2'
That clause doesn't reference any field from the "left" side of the join. I don't understand what it's supposed to accomplish, and I'm unsure whether the db engine will do what you want there.
Try this:
SELECT Employee.[First Name], Employee.[Last Name], Employee.[Email],
Departments.[Department]
FROM Employee
INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
INNER JOIN Departments ON EmpDept.[Department Number] = Departments.[Department Number]
/* Or whatever your foreign key between Departments and EmpDept is */
WHERE Departments.[Department Number] = '2'
AND Departments.[Campus] = 'Clarion'
If you have your heart set on a subquery, you need to alias it and make sure it forms a complete query on its own:
SELECT e.[First Name], e.[Last Name], e.Email, e.Department
FROM
(
SELECT Employee.[First Name], Employee.[Last Name], Employee.[Email],
Departments.[Department], Departments.Campus
FROM Employee
INNER JOIN EmpDept ON Employee.[EmpUserName] = EmpDept.[EmpUserName]
INNER JOIN Departments ON EmpDept.[Department Number] = Departments.[Department Number]
AND Departments.[Department Number] = '2'
) AS e
WHERE e.Campus = 'Clarion'

How do I use multiple IDs from a table with an INNER JOIN using SQL?

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

Entity Framework 4.0, adding SQL Server view which contains no primary key

I am using Entity Framework 4.0, C# 4.0, .Net 2010 and SQL Server 2008 R2. I have created the following view in my SQL Server database:
create view viewGetMember
as
select distinct
row_number() over (order by member.Membership_Number) ID,
email.Communication_Point_Id id1,
member.Membership_Number Number,
dvt.Default_Name ParticipationStatus,
person.Given_Name GivenName,
person.Last_Name LastName,
adrs.House_Number HouseNumber,
adrs.Street Street,
adrs.Post_Code PostCode,
email.Email_Address EmailAddress
from
Participation member
inner join
Party_Participation pp on member.Participation_Id = pp.Participation_Id
inner join
Party party on pp.Party_Id = party.Party_Id
inner join
Individual person on party.Party_Id = person.Party_Id
inner join
Domain_Value_t9n dvt on member.Participation_Status = dvt.Domain_Value_Id
inner join
Communication_Point cpadrs on party.Party_Id = cpadrs.Party_Id
inner join
Communication_Point cpemail on party.Party_Id = cpemail.Party_Id
inner join
[Address] adrs on cpadrs.Communication_Point_Id = adrs.Communication_Point_Id
inner join
Email email on cpemail.Communication_Point_Id = email.Communication_Point_Id
where
member.Membership_Number is not null
go
select * from viewGetMember
In want to add this view in entity framework. However it has contain no primary in it. Though following two fields can form a composite primary key (second and third columns).
email.Communication_Point_Id id1
member.Membership_Number Number
I don't know how to add them as part of Entity Framework. Even I have tried adding row_number() (first column) as an additional column by thinking it will act as a kind of primary key but no use. Entity Framework designer is not adding this view in .edmx model file.
I have tried this by entirely removing the .edmx file and in new project only for Entity Framework but no luck. Could someone please provide me a solution to this problem.
I found a perfect answer from the Entity Framework and Sql Server view question. According to the answer given to that question the above SQL Query (view without primary key) has to be changed like following.
create view viewGetMember as
select distinct
isnull(member.Membership_Number,-1) Number,
dvt.Default_Name ParticipationStatus,
person.Given_Name GivenName,
person.Last_Name LastName,
adrs.House_Number HouseNumber,
adrs.Street Street,
adrs.Post_Code PostCode,
email.Email_Address EmailAddress
from Participation member
inner join Party_Participation pp on member.Participation_Id = pp.Participation_Id
inner join Party party on pp.Party_Id = party.Party_Id
inner join Individual person on party.Party_Id = person.Party_Id
inner join Domain_Value_t9n dvt on member.Participation_Status = dvt.Domain_Value_Id
inner join Communication_Point cpadrs on party.Party_Id = cpadrs.Party_Id
and cpadrs.Communication_Point_Type in
(select dv.Domain_Value_Id from Domain_Value dv where dv.Short_Code = 'ADDRESS')
inner join Communication_Point cpemail on party.Party_Id = cpemail.Party_Id
and cpemail.Communication_Point_Type in
(select dv.Domain_Value_Id from Domain_Value dv where dv.Short_Code = 'EMAIL')
inner join Address adrs on cpadrs.Communication_Point_Id = adrs.Communication_Point_Id
inner join Email email on cpemail.Communication_Point_Id = email.Communication_Point_Id and cpemail.Is_Preferred = 1
where
member.Membership_Number is not null
go
select * from viewGetMember
go
The thrid line isnull(member.Membership_Number,-1) Number makes that column as primary key and we can get rid of row_number() over (order by member.Membership_Number) ID or whatever we don't want to be as part of primary key.
This works well in my case.

Categories