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#?
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 7 tables and each table will contain an entry for a particular product.I want to check whether all 7 tables contains entry for a particular ID(eg: 4562). ie, data exists or not.I am using SQL server 2008.Please help me to write a query to check the status.
Try the following command (example for 3 tables T1,T2,T3). It returns 1 if ID = 4562 exists in ALL tables and 0 if at least one table miss this ID.
SELECT
CASE WHEN
(
EXISTS(SELECT ID FROM T1 WHERE ID=4562)
AND EXISTS(SELECT ID FROM T2 WHERE ID=4562)
AND EXISTS(SELECT ID FROM T3 WHERE ID=4562)
)
THEN 1
ELSE 0
END AS [ID_Exists_in_all_tables]
SQLFiddle demo
If you do a basic join rather than left join, the product will only appear if it's in all of the tables.
select * from tab1
join tab2 on tab2.id = tab1.id
join tab3 on tab3.id = tab1.id
join tab4 on tab4.id = tab1.id
join tab5 on tab5.id = tab1.id
Where tab1.id = 1234
etc etc
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
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 following table structures.
**Table_A**
A_Id(BigInt) Category_Ids(varchar(50))
1 1,2,3
2 2,3
**Table_B**
B_Id(BigInt) C_Id(Bigint) Name(varchar(50))
1 2 A
2 1 C
3 3 B
First Query:
In this query want to get the record where A_Id=1. I have executed following code.
Select [Category_Ids] from Table_A where A_Id=1
This returns the data table with single column and single row with values “1, 2, 3”
Assume that above query fills the data into the A_datatable. I get the string from following code.
String ids = A_datatable.column[0][“Category_Ids”];
Second Query:
Now, I have to fetch the values from Table_B where C_Id in (1, 2, 3). I have executed following code and passed the string value to following query.
Select * from Table_B where C_Id in (ids)
When I execute above query getting the error, failed to convert parameter value from a String to a Int64.
You can actually do it in a single query.
SELECT b.*
FROM Table_A a
INNER JOIN Table_B b
ON ',' + a.Category_IDs + ',' LIKE '%,' + CAST(C_ID AS VARCHAR(10)) + ',%'
WHERE a.A_ID = 1
Caveat: This query is an index-killer. You should have properly normalize your tables.
UPDATE 1
Suggested Schema Design,
Table_A
A_ID
Category_ID
Table_C
B_ID
C_ID
Name
Records
Table_A
A_ID Category_ID
1 1
1 2
1 3
2 4
2 5
Table_B
B_Id C_Id Name
1 2 A
2 1 C
3 3 B
You can use a string splitter function like the one described here http://www.sqlservercentral.com/articles/Tally+Table/72993/
scroll down to the 'CREATE FUNCTION' script and run it to create your function, then you can split the comma separate string for use in your 'where in' clause
select * from Table_B
where C_Id in (select item from dbo.DelimitedSplit8K(IDS,','))