Linq query require for a join - c#

I am having two below tables. I need to unassigned rights for group 3 and my sql query is like below
select rightname
from IB_Right_Master
where id
not in (select RightID from IB_Group_Rights where GroupID = '3');
Table : RightMaster
ID | RightName | RightGroupName |
----------------------------------------------
1 | Test1 | test1 |
----------------------------------------------
2 | Test2 | test2 |
----------------------------------------------
3 | Test3 | Test3 |
----------------------------------------------
4 | Test4 | Group Test4 |
----------------------------------------------
Table : Group Rgihts
ID | RightID | GroupID |
----------------------------------------------
1 | 1 | 1 |
----------------------------------------------
2 | 1 | 2 |
---------------------------------------------
3 | 2 | 3 |
---------------------------------------------
4 | 3 | 4 |
---------------------------------------------
5 | 1 | 3 |
---------------------------------------------
Desired Output : for a group id 3
RightID | RightName |
-----------------------------
3 | Page Access |
------------------------------
4 | Delete Group |
-----------------------------

Try this query
from e in context.IB_Right_Master
where !(from e2 in context.IB_Group_Rights
where e2.GroupID == 3
select e2.RightID).ToList().Contains(e.id)
select e;

Related

How to query the sum of each person's availability base on their skill

I'm using sqlite, I've setup three tables.
Table for list of skills this can be expand.
+----+----------+
| ID | Skills |
+----+----------+
| 1 | Swimming |
| 2 | Running |
| 3 | Boxing |
| 4 | Dancing |
| 5 | Singing |
+----+----------+
Table for availability of each person. 0 represents the person is not available.
+-------+-----+-----+-----+-----+-----+-----+-----+
| Names | SUN | MON | TUE | WED | THU | FRI | SAT |
+-------+-----+-----+-----+-----+-----+-----+-----+
| Mark | 0 | 1 | 1 | 1 | 1 | 1 | 0 | (MON-FRI)
| Robin | 0 | 1 | 1 | 1 | 1 | 1 | 0 | (MON-FRI)
| James | 0 | 0 | 1 | 1 | 1 | 1 | 1 | (TUE-SAT)
+-------+-----+-----+-----+-----+-----+-----+-----+
Table for each person skill possess. 1/0 is a boolean value.
+-------+----------+---------+--------+---------+---------+
| Names | Swimming | Running | Boxing | Dancing | Singing |
+-------+----------+---------+--------+---------+---------+
| Mark | 1 | 1 | 1 | 1 | 1 |
| Robin | 1 | 1 | 1 | 1 | 1 |
| James | 1 | 1 | 1 | 1 | 0 |
+-------+----------+---------+--------+---------+---------+
By using the tables above how could I achieved this result to sum each person's availability base on their skill.
Expected Results:
+----------+-----+-----+-----+-----+-----+-----+-----+
| Skills | SUN | MON | TUE | WED | THU | FRI | SAT |
+----------+-----+-----+-----+-----+-----+-----+-----+
| Swimming | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Running | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Boxing | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Dancing | 0 | 2 | 3 | 3 | 3 | 3 | 1 |
| Singing | 0 | 2 | 3 | 3 | 3 | 3 | 0 |
+----------+-----+-----+-----+-----+-----+-----+-----+

How to simplify query boolean data in sqlite?

I have query that count the headcount base on assigned letter code per day.
I've used 3 tables;
TABLE :status as st
+----------------+---------------+--------+
| ID | status_name | status_code | status |
+----+-------------+-------------+--------+
| 1 | Available | A | true |
+------------------+-------------+--------+
| 2 | HalfDay | H | true |
+------------------+-------------+--------+
| 3 | On Leave | OL | true |
+------------------+-------------+--------+
| 4 | Restday | R | true |
+------------------+-------------+--------+
| 5 | Vacation | V | true |
+------------------+-------------+--------+
TABLE : employees as e
+--------------+-------+-------+------+----------+
| EmployeeName | Site | Shift | Team | JobTitle |
+--------------+-------+-------+------+----------+
| Steve | Bldg1 | Night | N1 | Doctor |
+--------------+-------+-------+------+----------+
| Dave | Bldg1 | Night | N2 | Nurse |
+--------------+-------+-------+------+----------+
| Jack | Bldg1 | Night | N2 | Nurse |
+--------------+-------+-------+------+----------+
| Jacob | Bldg2 | Day | D1 | Doctor |
+--------------+-------+-------+------+----------+
| Noah | Bldg2 | Day | D2 | Nurse |
+--------------+-------+-------+------+----------+
| MAX | Bldg2 | Day | D2 | Nurse |
+--------------+-------+-------+------+----------+
TABLE : schedule as sc
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| JobsType | Site | Shift | Team | SUN | MON | TUE | WED | THU | FRI | SAT |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Doctor | Bldg1 | Night | N1 | A | H | A | A | OL | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Nurse | Bldg1 | Night | N2 | A | H | H | A | A | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Doctor | Bldg2 | Day | D1 | H | A | H | H | A | A | OL |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Nurse | Bldg1 | Night | N2 | A | H | H | A | A | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
By using this query:
SELECT st.status_name, st.status_code
, sum(sc.SUN = st.status_code) AS SUN
, sum(sc.MON = st.status_code) AS MON
, sum(sc.TUE = st.status_code) AS TUE
, sum(sc.WED = st.status_code) AS WED
, sum(sc.THU = st.status_code) AS THU
, sum(sc.FRI = st.status_code) AS FRI
, sum(sc.SAT = st.status_code) AS SAT
FROM status AS st
JOIN schedule AS sc ON st.status_code IN (sc.SUN, sc.MON, sc.TUE, sc.WED
, sc.THU, sc.FRI, sc.SAT)
JOIN employees AS e ON sc.JobsType = e.JobTitle AND sc.Site = e.Site
AND sc.Shift = e.Shift AND sc.Team = e.Team
GROUP BY st.status_name, st.status_code
ORDER BY st.status_name, st.status_code;
I achieved this result:
+--------------+-----+-----+-----+-----+-----+-----+-----+
| STATUS TYPES | SUN | MON | TUE | WED | THU | FRI | SAT |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Available | 5 | 4 | 4 | 5 | 5 | 6 | 5 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| HalfDay | 1 | 5 | 5 | 1 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| On Leave | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Restday | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Vacation | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
-------------------------------------------------------------------
Now I'm having a hard time on how to work with a boolean value to reference.
Here are my tables;
TABLE: SkillList (1/0 = true/false)
+----------+--------+
| Skills | Status |
+----------+--------+
| Skill_1 | 1 |
+----------+--------+
| Skill_2 | 1 |
+----------+--------+
| Skill_3 | 1 |
+----------+--------+
| Skill_4 | 1 |
+----------+--------+
| Skill_5 | 0 |
+----------+--------+
TABLE: Skill Available (1/0 = true/false)
+----------+--------+---------+---------+---------+
| Username | Skill_1| Skill_2 | Skill_3 | Skill_4 |
+----------+--------+---------+---------+---------+
| Steve | 1 | 1 | 1 | 1 |
+----------+--------+---------+---------+---------+
| Dave | 1 | 0 | 1 | 0 |
+----------+--------+---------+---------+---------+
| Jack | 1 | 1 | 0 | 0 |
+----------+--------+---------+---------+---------+
| Jacob | 1 | 1 | 0 | 0 |
+----------+--------+---------+---------+---------+
Note: Zero represents users that doesn't have that skill.
TABLE: Attendance (1/0 = true/false)
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Username | Site | Shift | SUN | MON | TUE | WED | THU | FRI | SAT |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Steve | Bldg1 | Night | 1 | 1 | 1 | 1 | 1 | 0 | 0 |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Dave | Bldg1 | Night | 1 | 1 | 0 | 0 | 1 | 1 | 1 |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Jack | Bldg2 | Day | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
| Jacob | Bldg1 | Night | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
+----------+-------+-------+-----+-----+-----+-----+-----+-----+-----+
Note: Zero represents restday.
By using the tables above how could I achieved this result that count available user per day base on there skill?
+-----------+-----+-----+-----+-----+-----+-----+-----+
| SkillList | SUN | MON | TUE | WED | THU | FRI | SAT |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_1 | 4 | 3 | 2 | 2 | 3 | 3 | 3 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_2 | 3 | 3 | 1 | 1 | 2 | 2 | 2 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_3 | 2 | 2 | 1 | 0 | 2 | 1 | 1 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_4 | 1 | 1 | 0 | 0 | 1 | 0 | 0 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
| Skill_5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+-----------+-----+-----+-----+-----+-----+-----+-----+
SQL databases are all about modelling relationships between data, and there are well established ways to do so. In this case, you have many users, each of which may have many skills, - a many to many relationship. The way to model this is known by many names, but I prefer junction table. Basically, instead of a table with a column for each skill and a row for each user, have a table of (skill id, user id) pairs with a row for each particular combination. If a user doesn't have a skill, no row with that particular combination exists.
Setting up some example tables from your data to demonstrate the idea:
CREATE TABLE SkillList(id INTEGER PRIMARY KEY, skill TEXT);
INSERT INTO SkillList VALUES
(1, 'Skill 1'), (2, 'Skill 2'), (3, 'Skill 3'), (4, 'Skill 4'), (5, 'Skill 5');
CREATE TABLE Attendance(id INTEGER PRIMARY KEY, username TEXT UNIQUE
, site TEXT, shift Text, SUN INTEGER, MON INTEGER
, TUE INTEGER, WED INTEGER, THU INTEGER, FRI INTEGER
, SAT INTEGER);
INSERT INTO Attendance VALUES
(1, 'Steve', 'Bldg1', 'Night', 1, 1, 1, 1, 1, 0, 0),
(2, 'Dave', 'Bldg1', 'Night', 1, 1, 0, 0, 1, 1, 1),
(3, 'Jack', 'Bldg2', 'Day', 1, 1, 1, 0, 0, 1, 1),
(4, 'Jacob', 'Bldg1', 'Night', 1, 0, 0, 1, 1, 1, 1);
CREATE TABLE SkillsAvailable(skill_id INTEGER REFERENCES SkillList(id)
, user_id INTEGER REFERENCES Attendance(id)
, PRIMARY KEY(skill_id, user_id)) WITHOUT ROWID;
INSERT INTO SkillsAvailable VALUES
(1, 1), (1, 2), (1, 3), (1, 4),
(2, 1), (2, 3), (2, 4),
(3, 1), (3, 2),
(4, 1);
will let you join the SkillList and Attendance tables together by putting SkillsAvailable in the middle:
SELECT sl.skill AS "Skill Name"
, ifnull(sum(a.SUN), 0) AS SUN
, ifnull(sum(a.MON), 0) AS MON
, ifnull(sum(a.TUE), 0) AS TUE
, ifnull(sum(a.WED), 0) AS WED
, ifnull(sum(a.THU), 0) AS THU
, ifnull(sum(a.FRI), 0) AS FRI
, ifnull(sum(a.SAT), 0) AS SAT
FROM SkillList AS sl
LEFT OUTER JOIN SkillsAvailable AS sa ON sl.id = sa.skill_id
LEFT OUTER JOIN Attendance AS a ON sa.user_id = a.id
GROUP BY sl.id
ORDER BY sl.skill;
Outer joins are used so that skills that aren't used by anyone still show up in the results, which are:
Skill Name SUN MON TUE WED THU FRI SAT
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Skill 1 4 3 2 2 3 3 3
Skill 2 3 2 2 2 2 2 2
Skill 3 2 2 1 1 2 1 1
Skill 4 1 1 1 1 1 0 0
Skill 5 0 0 0 0 0 0 0
Besides making this sort of calculations more complex, your current database layout has other issues with the "Skills Available" table - adding a new skill means adding a new column, removing a skill you don't care about means either deleting that column - a very convoluted process in sqlite - or having unused ones hanging around wasting some space. It makes for a fragile, over complicated design. Better to play to the strengths of relational databases.

How to simplify query from excel to sqlite?

How to achieved the same result in sqlite?
In excel I have this which count the headcount base on assigned letter code per day:
Here's my excel formula: =SUMIF(G$2:G$5,E9,$E$2:$E$5)
In my Sqlite database I have 3 tables:
TABLE 1
+----------------+---------------+--------+
| ID | status_name | status_code | status |
+----+-------------+-------------+--------+
| 1 | Available | A | true |
+------------------+-------------+--------+
| 2 | HalfDay | H | true |
+------------------+-------------+--------+
| 3 | On Leave | OL | true |
+------------------+-------------+--------+
| 4 | Restday | R | true |
+------------------+-------------+--------+
| 5 | Vacation | V | true |
+------------------+-------------+--------+
TABLE 2
+--------------+-------+-------+------+----------+
| EmployeeName | Site | Shift | Team | JobTitle |
+--------------+-------+-------+------+----------+
| Steve | Bldg1 | Night | N1 | Doctor |
+--------------+-------+-------+------+----------+
| Dave | Bldg1 | Night | N2 | Nurse |
+--------------+-------+-------+------+----------+
| Jack | Bldg1 | Night | N2 | Nurse |
+--------------+-------+-------+------+----------+
| Jacob | Bldg2 | Day | D1 | Doctor |
+--------------+-------+-------+------+----------+
| Noah | Bldg2 | Day | D2 | Nurse |
+--------------+-------+-------+------+----------+
| MAX | Bldg2 | Day | D2 | Nurse |
+--------------+-------+-------+------+----------+
TABLE 3
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| JobsType | Site | Shift | Team | SUN | MON | TUE | WED | THU | FRI | SAT |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Doctor | Bldg1 | Night | N1 | A | H | A | A | OL | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Nurse | Bldg1 | Night | N2 | A | H | H | A | A | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Doctor | Bldg2 | Day | D1 | H | A | H | H | A | A | OL |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
| Nurse | Bldg1 | Night | N2 | A | H | H | A | A | A | A |
+----------+-------+-------+------+-----+-----+-----+-----+-----+-----+-----+
By using the 3 tables above how could I achieved this result in query?
+--------------+-----+-----+-----+-----+-----+-----+-----+
| STATUS TYPES | SUN | MON | TUE | WED | THU | FRI | SAT |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Available | 5 | 4 | 4 | 5 | 5 | 6 | 5 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| HalfDay | 1 | 5 | 5 | 1 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| On Leave | 0 | 0 | 0 | 0 | 1 | 0 | 1 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Restday | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
| Vacation | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
+--------------+-----+-----+-----+-----+-----+-----+-----+
So far I've come this;
SELECT DISTINCT M.status_name, M.status_code, A.SUN, count(*),count(*) FROM TABLE3 M LEFT join TABLE2 A ON M.status_code= A.SUN LEFT join TABLE2 B ON (A.Team = B.Team AND A.Shift = B.Shift AND A.Site = B.Site)
WHERE M.product_status = 1
GROUP BY M.status_name;
First step, generate a test database (With a couple of helpful indexes):
CREATE TABLE status(id INTEGER PRIMARY KEY, status_name TEXT, status_code TEXT, status TEXT);
INSERT INTO status VALUES(1,'Available','A','true');
INSERT INTO status VALUES(2,'HalfDay','H','true');
INSERT INTO status VALUES(3,'On Leave','OL','true');
INSERT INTO status VALUES(4,'Restday','R','true');
INSERT INTO status VALUES(5,'Vacation','V','true');
CREATE TABLE employees(EmployeeName TEXT, Site TEXT, Shift TEXT, Team TEXT, JobTitle TEXT);
INSERT INTO employees VALUES('Steve','Bldg1','Night','N1','Doctor');
INSERT INTO employees VALUES('Dave','Bldg1','Night','N2','Nurse');
INSERT INTO employees VALUES('Jack','Bldg1','Night','N2','Nurse');
INSERT INTO employees VALUES('Jacob','Bldg2','Day','D1','Doctor');
INSERT INTO employees VALUES('Noah','Bldg2','Day','D2','Nurse');
INSERT INTO employees VALUES('MAX','Bldg2','Day','D2','Nurse');
CREATE TABLE schedule(JobsType TEXT, Site TEXT, Shift TEXT, Team TEXT, SUN TEXT, MON TEXT, TUE TEXT, WED TEXT, THU TEXT, FRI TEXT, SAT TEXT);
INSERT INTO schedule VALUES('Doctor','Bldg1','Night','N1','A','H','A','A','OL','A','A');
INSERT INTO schedule VALUES('Nurse','Bldg1','Night','N2','A','H','H','A','A','A','A');
INSERT INTO schedule VALUES('Doctor','Bldg2','Day','D1','H','A','H','H','A','A','OL');
INSERT INTO schedule VALUES('Nurse','Bldg1','Night','N2','A','H','H','A','A','A','A');
CREATE INDEX employees_idx ON employees(JobTitle, Site, Shift, Team);
CREATE INDEX status_idx_code ON status(status_code);
This query:
SELECT st.status_name, st.status_code
, sum(sc.SUN = st.status_code) AS SUN
, sum(sc.MON = st.status_code) AS MON
, sum(sc.TUE = st.status_code) AS TUE
, sum(sc.WED = st.status_code) AS WED
, sum(sc.THU = st.status_code) AS THU
, sum(sc.FRI = st.status_code) AS FRI
, sum(sc.SAT = st.status_code) AS SAT
FROM status AS st
JOIN schedule AS sc ON st.status_code IN (sc.SUN, sc.MON, sc.TUE, sc.WED
, sc.THU, sc.FRI, sc.SAT)
JOIN employees AS e ON sc.JobsType = e.JobTitle AND sc.Site = e.Site
AND sc.Shift = e.Shift AND sc.Team = e.Team
GROUP BY st.status_name, st.status_code
ORDER BY st.status_name, st.status_code;
will produce
status_name status_code SUN MON TUE WED THU FRI SAT
----------- ----------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
Available A 5 1 1 5 5 6 5
HalfDay H 1 5 5 1 0 0 0
On Leave OL 0 0 0 0 1 0 1
How it works:
For each row in the status table, join each row in the schedule table that has that status for at least one day, and then join all rows from the employee table that match each schedule row's type of job (With this sample data, that results in a total of 14 rows being generated). Then group all the rows on the status, and for each day, add up the number of rows in that group where the schedule code for that day matches the status.

Convert T-sql to Linq or Entityframework and not to use SqlQuery() for raw queries

I have a table which is called Signer :
+--------------+----------+---------+--------+--------------+---------+-----------+
| Name | User | Order | Signed | CompanyName | Status | InvoiceId |
+--------------+----------+---------+--------+--------------+---------+-----------+
| Anders | aa | 1 | 0 | OvnAnd2 | 0 | 26650 |
| Peyman | pm | 2 | 1 | OvnAnd2 | 1 | 26650 |
| Siw Ericsson | se | 3 | 0 | OvnAnd2 | 0 | 26650 |
| test | test | 4 | 0 | OvnAnd2 | 0 | 26650 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 26652 |
| test | test | 2 | 1 | OvnAnd2 | 0 | 26652 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 25365 |
+--------------+----------+---------+--------+--------------+---------+-----------+
Goal:
As you can see I have 3 different InvoiceId's. For each InvoiceId, I would like to find a row with minimum order number that Status column's value is 0 and User column has to be se.
( It means, Show the current users related invoices which are ready to be signed based on his/her username, order, signed columns)
I came up with this T-SQL which works fine :
select * from Signer s1
where s1.User = 'se' and Order = (select min(Order) from Signer s2 where s2.InvoiceId = s1.InvoiceId and Signed = 0)
And the result:
+--------------+----------+---------+--------+--------------+---------+-----------+
| Name | User | Order | Signed | CompanyName | Status | InvoiceId |
+--------------+----------+---------+--------+--------------+---------+-----------+
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 26652 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 25365 |
+--------------+----------+---------+--------+--------------+---------+-----------+
I would like to convert this query from T-Sql to Linq or EntityFramework :
This is my solution:
var temp = from x in db.Signers
where x.User == Me.UserName &&
x.Signed == 0
group x by x.InvoiceId
into item
select new
{
item.Key,
item = item.Min(x => x.Order)
};
Which returns 3 rows which is wrong because Siw should see the related invoices that are ready to be signed by her. (It means the first row should not be in the list)
+--------------+----------+---------+--------+--------------+---------+-----------+
| Name | User | Order | Signed | CompanyName | Status | InvoiceId |
+--------------+----------+---------+--------+--------------+---------+-----------+
| Anders | aa | 1 | 0 | OvnAnd2 | 0 | 26650 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 26652 |
|Siw Ericsson | se | 1 | 0 | OvnAnd2 | 0 | 25365 |
+--------------+----------+---------+--------+--------------+---------+-----------+
More info:
- As you can see in the first table, we have a special logic that someone can sign invoices out of order and Peyman is one of them.
- I don't want to use SqlQuery() method in Entityframework in order to execute t-sql queries.
I appreciate any help to find a solution for my goal.
Best regards
I think this is one of those situations where "let" come in handy:
var result = from s in Signers
let minToSign = Signers.Where(si =>
si.InvoiceId == s.InvoiceId && si.Signed == 0
).Min(si => si.Order)
where s.User == "se" && s.Order == minToSign
select s;

MySQL Query to retrieve data from two tables even no data found and unique column in both tables

I have two tables.
Menu
+----------------+----------------+
| menu_id | menu_desc |
+----------------+----------------+
| 1 | menu1 |
| 2 | menu2 |
| 3 | menu3 |
| 4 | menu4 |
| 5 | menu5 |
+----------------+----------------+
Rights
+----------+--------------+---------+
| Role_id | menu_id | Rights |
+----------+--------------+---------+
| 1 | 1 | 3 |
| 1 | 2 | 3 |
| 1 | 3 | 3 |
+----------+--------------+---------+
I want the output something like this,
+----------+------------------+------------+-----------+
| menu_id | menu_desc | Role_id | Rights |
+----------+------------------+------------+-----------+
| 1 | menu1 | 1 | 3 |
| 2 | menu2 | 1 | 3 |
| 3 | menu3 | 1 | 3 |
| 4 | menu4 | 1 | null |
| 5 | menu5 | 1 | null |
+----------+------------------+------------+-----------+
Is it possible?
This gives NULL for both Role_id and Rights
SELECT Menu.menu_id, Menu.menu_desc, Role.Role_id, Role.Rights
FROM Menu LEFT OUTER JOIN Role ON Menu.menu_id=Role.menu_id
ORDER BY Menu.menu_id
try below, if not getting required results then share problem:
SELECT m.menu_id, m.menu_desc, r.Role_id, r. Rights FROM Menu m LEFT JOIN `Rights` r ON m.menu_id=r.menu_id order by m.menu_id;
You have to use join:
select Menu.menu_id, menu_desc, Role_id, Rights.menu_id, Rights.Rights from Menu join Rights on Menu.menu_id=Rights.menu_id;

Categories

Resources