Here is 2 Tables that are joined by the StaffID
Job Table
=========
JobID AssignedTo(StaffID) Created By(StaffID)
1 2 1
2 3 2
Staff Table
============
StaffID Name
1 May
2 Bob
3 Mary
I need An SQL Statement to get the job details with the corresponding staff name but have problems doing so as i'm unable to differentiate the columns as they are using the same table. The end result should look like this
JobID Assigned To Created By
1 Bob May
2 Mary Bob
You need to join Staff table twice
select J.JobId, S1.Name AS AssignedTo, S2.Name AS CreatedBy
from Job J
inner join Staff S1 on S1.StaffID = J.AssignedTo
inner join Staff S2 on S2.StaffID = J.CreatedBy
Related
I'm trying to determine the following from the joining of two tables:
Sample TableA: Contains 1 column consisting of unique IDs
ID Name Department
1 John IT
2 Jason Sales
3 Dany IT
4 Mike HR
5 Alex HR
Sample TableB: Contains multiple columns, including ID from TableA. For example:
ID AccountNumber WebID
1 10725 ABC1
1 10726 ABC1
1 10727 ABC1
2 20100 ABC2
2 20101 ABC2
3 30100 ABC3
4 40100 NULL
I want to get the following results:
ID Name WebID
1 John ABC1
2 Jason ABC2
3 Dany ABC3
4 Mike NULL
5 Alex NULL
I tried the following query, which is returning the correct rows for these sample tables:
Select count(a.ID), a.ID, a.Name, b.WebID from TableA a
left join TableB b on a.ID = b.ID
group by a.ID, a.Name, b.WebID
But my Actual Database tables, this query does not return correct number of rows: (30992)
TableA contains 29066 rows and TableB contains 23033 rows
The query should return 29066 rows, as it is Left Join.
When I checked the IDs that are in TableA, but not in TableB, there were 6033 rows:
Select * from TableA where ID not in (Select ID from TableB)
Am I missing something in the query?
TABLE B has duplicates of the ID column... the code below should work (but might not be the results you expect since I just do a max on the webid column which is fine if it is always the same but I need a rule if not)
I just saw you had a count... I added that in.
SELECT A.ID, A.Name, B.WebID
FROM TABLEA A
LEFT JOIN (
SELECT ID, MAX(WebID) AS WebID, count(*) as CNT
FROM TABLEB
GROUP BY ID
) B ON A.ID = B.ID
I think your query is as simple as that:
select a.ID,a.Name,b.WebID
from TableA a
left join TableB b on a.ID = b.ID
I have location data coming from DB. There is parent-Child Relationship in my Location table.
I retrieved the records. But problem is how can I populate my View Model.
For example
Location 'US' has child 'California' and 'New York', and they have their own child.
I am not using any mapping tool.
Here is the sample data
Id ChildId Name
1 null US
2 null Canada
3 1 California
4 3 Los Angeles
5 2 Nova Scotia
6 5 Halifax
And I dont know the depth of my Location table's parent-Child relation ship
I like some solution so when I write
List<LocVM> obj=context.locations.select(w=new LocVM{....,ChileLocation=w.ChildLoc}).tolist();
so my object will have all locations and child locations
View Model
Class LocVM{
public int Id{get;set;}
public int chileId {get;set;}
public LocVM ChildLocations {get;set;}
}
So with query above
Any idea is welcome
Sample Data
IF OBJECT_ID('dbo.SampleData') IS NOT NULL
DROP TABLE SampleData
CREATE TABLE SampleData (ID int,ChildId INT,Name VARCHAR(20))
INSERT INTO SampleData
SELECT 1,null,'US' UNION ALL
SELECT 2,null,'Canada' UNION ALL
SELECT 3,1 ,'California' UNION ALL
SELECT 4,3 ,'Los Angeles' UNION ALL
SELECT 5,2 ,'Nova Scotia' UNION ALL
SELECT 6,5 ,'Halifax'
SELECT * FROM SampleData
This Recursive CTE May be able to solve your requirement.
;With Cte
AS
(
SELECT
ID
,ChildId
,CAST('/'+Name AS nvarchar(1000)) AS [Heirarchy]
FROM SampleData
WHERE ChildId IS NULL
UNION ALL
SELECT
e.ID
,e.ChildId
,CAST([Heirarchy]+'/'+e.Name AS nvarchar(1000)) AS [Heirarchy]
FROM SampleData e
INNER JOIN Cte t
ON t.ID = e.ChildId
)
SELECT ID
,RIGHT([Heirarchy],LEN([Heirarchy])-1) AS [CityHeirarchyPath]
FROM Cte
Result
ID CityHeirarchyPath
----------------------
1 US
2 Canada
5 Canada/Nova Scotia
6 Canada/Nova Scotia/Halifax
3 US/California
4 US/California/Los Angeles
Hello this is what i have in mind but no idea how to properly do it.
Table1
ID ID2 Name Dosage
------------------------------
1 001 Name1 Dosage1
2 002 Name2 Dosage2
3 003 Name3 Dosage3
Table2
ID Quantity
------------------------
1 1000
2 2000
3 3000
Query something like:
Select ID,Name,Dosage from Table1 and Quantity(of the same ID from Table1)from Table2
where ID2 from Table1 ='002';
Datagridview Output
ID Name Dosage Quantity
---------------------------------
2 Name2 Dosage2 2000
A simple SQL join should work.
Try this :
select Table1.ID, Table1.Name, Table1.Dosage, Table2.Quantity
from Table1
inner join Table2 on Table2.ID = Table1.ID
where Table2.ID2 = '002';
From the comment you gave to the another answer. You should fix it to look like this:
DECLARE #SupID INT = 200049;
SELECT SP.ProductID, SP.Brand, SP.Dosage, P.Quantity
FROM Supplier_productlist AS SP
INNER JOIN Products AS P
ON P.ID = SP.ProductID
WHERE SP.SupplierID = #SupID;
In case SupplierID is a "string", declare it like this instead:
DECLARE #SupID NVARCHAR(10) = '200049';
Dear Friends,
i want to select two columns from two different tables in the same db using mysql and set the output of the query to a variable in c#.
currently my code is as shown below:
MySqlCommand logcmdCheck = new MySqlCommand(query, connectionCheck);
string query = "SELECT DB.table1.column1,DB.table1.column2,DB.table2.column1,DB.table2.column2,DB.table2.column3 FROM DB.table1 WHERE DB.table1.column1=?x,DB.table2 WHERE DB.table2.column1=?y";
logcmdCheck.Parameters.AddWithValue("?x",UserName);
logcmdCheck.Parameters.AddWithValue("?y",emailID);
MySqlDataReader ldr = logcmdCheck.ExecuteReader();
A = ldr[0].ToString();
B = ldr[1].ToString();
C = ldr[2].ToString();
D = ldr[3].ToString();
E = ldr[4].ToString();
Error: Mysql query syntax is wrong.
Kindly please help me out with the mysql command to perform the requirement.
Thanks in advance
Suraj
You're going to have to use a SQL Join. Check it out here http://www.w3schools.com/sql/sql_join.asp. You need to have a foreign key in one of the tables that allows you to connect to the primary key of the other table. Every good database should be set up with tables that have foreign keys.
For example:
Table 1:
OrderNumber Name Order Total
1 John Smith 10.00
2 Sally Smith 5.00
3 Berry Jones 25.00
Table 2:
Item Number ItemTotal OrderNumber
1 5.00 1
2 5.00 1
3 2.50 2
4 2.50 2
5 25.00 3
In table 2 the OrderNumber is the foreign key that is able to join to table one. So your syntax would be:
SELECT * FROM table1 JOIN table2 ON table2.OrderNumber = table1.OrderNumber
That will give you one table which you can read from.
I have 3 table how to get the value of 3rd table by using join in MySql
table1
RowID UserID RoleID
1 1 2
2 171 3
table2
RowID RoleID PermissionID
1 2 2
2 2 3
3 3 14
4 3 15
table3:
PermissionID PermissionName
2 Edit organisation
3 Delete organisation
14 Create group
15 Edit group
16 Delete group
Here I will know only the UserID, if suppose the UserID is 171, then I should get the roleid(3) from table1 and get PermissionID(14,15) from table 2 and then get the PermissionName(Create group, Edit group) from table 3 and I have to store it in a list. How can I do it. I am using c# and mysql. Thanks
SQL Query to select from DB:
SELECT p.PermissionID, p.PermissionName
FROM users u
INNER JOIN roles r ON r.RoleID = u.RoleID
INNER JOIN permissions p ON p.PermissionID = r.PermissionID
Next step: (But use ExecuteReader() instead of ExecuteNonQuery())
MSDN: ExecuteReader() example
Executing an SQL statement in C#?