Class_List table
Class_List_id
Class_id
341
4
342
4
Lesson_CLass table
Class_List_id
Lesson_id
341
22
342
22
I am trying to work out a NOT EXIST IN query, which finds all the Class_List_ids in the Class_List table that do not exist in the Lesson_CLass table where the Lesson_id is 21 and class ID is 4.
SELECT Class_List.class_id, Class_List.Class_List_id
FROM Class_List WHERE NOT EXISTS (SELECT
Class_List.class_id, Class_List.Class_List_id,
Lesson_CLass.Class_List_id, Lesson_CLass.Lesson_id
FROM Class_List, Lesson_CLass where
(Class_List.Class_List_id=Lesson_CLass.Class_List_id) AND
(Lesson_CLass.Lesson_id=21) AND (Class_List.Class_id=4));
This results in all the Class_List records rather than the ones that don't exist.
You can use a LEFT JOIN for this purpose
SELECT cl.class_id, cl.Class_List_id
FROM Class_List cl
left join Lesson_CLass lc
on cl.Class_List_id = lc.Class_List_id
and (lc.Lesson_id = 21 and cl.Class_id = 4)
where lc.Class_List_ids is null;
Related
Say I have three tables as such:
Group Table
OGID
OGC
OGCD
56
300
TAS
81
TA
CAL
Structure Table
OSID
L1D
L2D
44
56
81
Contract
ContractID
44
Im giving the ContractID and I want to create a Table that has the follow:
ContractID
Structure L1D
Structure L2D
Group OGID
Group OGC
Group OGCD
Group OGID
Group OGC
Group OGCD
44
56
81
56
300
TAS
81
TA
CAL
What would be the best way to go about this in SQL?
There is also the problem that L2D can be null and anytime I try to make INNER JOIN statements to join the tables, the NULL ones are ignore.
SELECT
Contract.ContractID, Structure.L1D, Structure L2D, Group.OGID, Group.OGC, Group.OGID, Group2.OGID, Group2.OGC, Group2.OGID,
FROM
(
SELECT Structure.OSID, Structure.L1d, Group.OGID, Group.OGC, Group.OGCD
FROM Structure
INNER JOIN Group
ON (Structure.L1D = Group.OGID)
) T1
INNER JOIN
(
SELECT Structure.OSID, Structure.L1D, Group2.OGID, Group2.OGC, Group2.OGCD
FROM Structure
INNER JOIN Group2
ON (Structure.L2D = Group2.OGID)
) T2
ON (T1.OSID = T2.OSID OR T2.OSID = NULL)
See DBFIDDLE
SELECT
s.OSID,
s.L1D,
s.L2D,
g1.OGID,
g1.OGC,
g1.OGCSD,
g2.OGID,
g2.OGC,
g2.OGCSD
FROM structure s
LEFT JOIN `group` g1 ON g1.OGID = s.L1D
LEFT JOIN `group` g2 ON g2.OGID = s.L2D
The DBFIDDLE also shows what happens when L2D has the value NULL.
P.S. Generally you should not use, or at least try to avoid, Reserved Words as table names, like GROUP.
T1 Customers
IDZ NAME MEGAID
123 TOM 32132
124 JEK 32323
125 MAX 32342
126 JIZ 32134
T2 Info:
ID CID GUNS STATUS
1 123 3 1
2 124 4 2
3 126 NULL 1
T3 Status:
ID TYPE
1 Active
2 Inactive
IDZ = CID
I need to return NAME, MEGAID and STATUS (Active/Inactive) for everyone who have NULL on GUNS column from INFO table.
I tried this:
SELECT Customers.Name, CustomersMEGAID, Status.TYPE
FROM Customers
LEFT JOIN Customers ON Info.CID=Custoners.IDZ
WHERE Info.Guns= IS NULL;
But thats doesnt work(
Big thanks if someone can help with this
your question is full with errors but here is a query for you:
SELECT Customers.Name, Customers.MEGAID, Status.TYPE
FROM Customers
LEFT JOIN Info ON Customers.IDZ = Info.CID
INNER JOIN Status ON Info.STATUS = Status.ID
WHERE Info.Guns IS NULL;
You can join all three tables, and then search for nulls in the column.
For example:
select
c.name,
c.megaid,
s.type
from customers c
join info i on i.cid = c.idz
join status s on s.id = i.status
where i.guns is null
I have a database with a one to many relationship (venues, with associated categories) like so:
venue_id | venue_name
---------------------
1 | venue1
2 | venue2
and categories
venue_id | category_id
---------------------
1 | 5
2 | 7
1 | 8
2 | 5
I want to show all venues that HAVE category_id of 5, but dont have category_id of 7 and 8. I tried using a join like so:
SELECT distinct(`venue_to_category`.`venue_id`),`venue_name`
FROM `venue_to_category` INNER JOIN `venues`
ON `venues`.venue_id = `venue_to_category`.venue_id
WHERE `category_id` != 7
AND `category_id` != 8
AND `category_id` = 5
But it is not returning the correct results (in fact I am unsure the results it is returning)
2 Things in your case
Get data where category_id = 5 and not 7 or 8
Get data where category_id = 5 and not both 7 and 8
For the first one you can use
select
v.venue_id,
v.venue_name
from venues v
join categories c on c.venue_id = v.venue_id
where c.category_id = 5
AND NOT EXISTS
(
select 1 from categories c1 where v.venue_id = c1.venue_id
AND c1.category_id in (7,8)
);
For the 2nd one
select
v.venue_id,
v.venue_name
from venues v
join categories c on c.venue_id = v.venue_id
where c.category_id = 5
AND NOT EXISTS
(
select 1 from categories c1 where v.venue_id = c1.venue_id
AND c1.category_id in (7,8) having count(*) = 2
);
DEMO
When doing a search function of your site. Match and Against is better than Like sql statement. The field must be set to FullText match the term on the field:
SELECT vc.venue_id, venue_name FROM venue_to_category vc, venues v WHERE MATCH(category_id) AGAINST('-7 -8 +5');
You can also use the IN BOOLEAN MODE to allow operators in the sql statement. eg. ...
MATCH(category_id) AGAINST('-7 -8 +5' IN BOOLEAN MODE) ...
(-) minus sign that means nothing should match '-7' '-8'
(+) the word must be present in the match.
There are many other operators to be used. Refer to this page for more operator and explanation
Try
SELECT distinct `venue_to_category`.`venue_id` ,`venue_name`
FROM `venue_to_category` INNER JOIN `venues`
ON `venues`.venue_id = `venue_to_category`.venue_id
WHERE `category_id` <> 7
AND `category_id` <> 8
AND `category_id` = 5
or
SELECT distinct `venue_to_category`.`venue_id` ,`venue_name`
FROM `venue_to_category` INNER JOIN `venues`
ON `venues`.venue_id = `venue_to_category`.venue_id
WHERE `category_id` NOT IN (7,8)
AND `category_id` = 5
Note: distinct is not a function. And for your example you could leave it out entirely, because there are no duplicate rows.
SELECT v.id, v.name
FROM venues v
JOIN venue_to_category vc
ON vc.venue_id=v.id
AND vc.category_id=5
WHERE NOT EXISTS (
SELECT 1
FROM venue_to_category vci
WHERE vci.venue_id = v.id
AND vci.category_id IN (7,8)
);
I need the records where pid is NULL from the following result set:
pid users_fee_schedule_students_name users_fee_schedule_students_uid
8 users 6 MBA in IT 1 1337097600 3 6 user 250000
9 users 6 MBA in IT 2 1337184000 3 7 user 250000
NULL ashuser 277 MBA in IT 1 1337097600 3 6 user 250000
NULL ashuser 277 MBA in IT 2 1337184000 3 7 user 250000
10 sriuser 66 MBA in IT 1 1337097600 3 6 user 250000
NULL sriuser 66 MBA in IT
I SQL query to get the above result set is
SELECT FSP.pid, users_fee_schedule_students.name AS users_fee_schedule_students_name, users_fee_schedule_students.uid AS users_fee_schedule_students_uid, fee_schedule.name AS fee_schedule_name, fee_schedule_instalments.instalment_no AS fee_schedule_instalments_instalment_no, fee_schedule_instalments.payable_by AS fee_schedule_instalments_payable_by, fee_schedule.fid AS fid, fee_schedule_instalments.iid AS fee_schedule_instalments_iid, 'user' AS field_data_field_school_course_user_entity_type, SUM(fee_schedule_instalments.amount) AS fee_schedule_instalments_amount
FROM
ic_fee_schedule AS fee_schedule
LEFT JOIN ic_fee_schedule_students AS fee_schedule_students ON fee_schedule.fid = fee_schedule_students.fid
LEFT JOIN ic_users AS users_fee_schedule_students ON fee_schedule_students.uid = users_fee_schedule_students.uid
LEFT JOIN ic_fee_schedule_instalments AS fee_schedule_instalments ON fee_schedule.fid = fee_schedule_instalments.fid
LEFT JOIN ic_fee_schedule_payments AS FSP ON fee_schedule.fid = FSP.fid AND fee_schedule_students.uid = FSP.uid AND fee_schedule_instalments.iid = FSP.iid
WHERE (( (fee_schedule.fid = '3' ) ))
GROUP BY users_fee_schedule_students_name, users_fee_schedule_students_uid, fee_schedule_name, fee_schedule_instalments_instalment_no, fee_schedule_instalments_payable_by, fid , fee_schedule_instalments_iid
I think
LEFT JOIN ic_fee_schedule_payments AS FSP need to change with some other type of join. Checking on it.....
Thanks in advance for your help.
Tried this:
SELECT * FROM TableA
LEFT OUTER JOIN TableB
ON TableA.name = TableB.name
WHERE TableB.id IS null
To produce the set of records only in Table A, but not in Table B, we perform the same left outer join, then exclude the records we don't want from the right side via a where clause.
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
I believe you can simply add a WHERE clause "AND FSP.pid IS NULL" since you're doing a left join on FSP.
Try this:
SELECT FSP.pid, users_fee_schedule_students.name AS users_fee_schedule_students_name, users_fee_schedule_students.uid AS users_fee_schedule_students_uid, fee_schedule.name AS fee_schedule_name, fee_schedule_instalments.instalment_no AS fee_schedule_instalments_instalment_no, fee_schedule_instalments.payable_by AS fee_schedule_instalments_payable_by, fee_schedule.fid AS fid, fee_schedule_instalments.iid AS fee_schedule_instalments_iid, 'user' AS field_data_field_school_course_user_entity_type, SUM(fee_schedule_instalments.amount) AS fee_schedule_instalments_amount
FROM ic_fee_schedule AS fee_schedule
LEFT JOIN ic_fee_schedule_students AS fee_schedule_students ON fee_schedule.fid = fee_schedule_students.fid
LEFT JOIN ic_users AS users_fee_schedule_students ON fee_schedule_students.uid = users_fee_schedule_students.uid
LEFT JOIN ic_fee_schedule_instalments AS fee_schedule_instalments ON fee_schedule.fid = fee_schedule_instalments.fid
LEFT JOIN ic_fee_schedule_payments AS FSP ON fee_schedule.fid = FSP.fid AND fee_schedule_students.uid = FSP.uid AND fee_schedule_instalments.iid = FSP.iid
WHERE fee_schedule.fid = '3'
AND FSP.pid IS NULL
GROUP BY users_fee_schedule_students_name, users_fee_schedule_students_uid, fee_schedule_name, fee_schedule_instalments_instalment_no, fee_schedule_instalments_payable_by, fid , fee_schedule_instalments_iid
I am trying to use TWICE inner join statement to get reference values in the same detail table.
Master table: bags_tbl
ID ... bagA bagB
1 ... 121 122
2 ... 123 124
3 ... 125 126
Detail table: fruit_tbl
ID ... fruit ...
121 strawbery
122 apple
123 orange
124 raspberry
125 pear
126 pineapple
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
fruit_tbl.fruit AS bagA_fruit,
fruit_tbl.fruit AS bagB_fruit
FROM
bags_tbl
Inner Join fruit_tbl ON bags_tbl.bagA = fruit_tbl.fruit
Inner Join fruit_tbl ON bags_tbl.bagB = fruit_tbl.fruit
this throw error : no unique table/alias...
How to make SQL statement to get text representation of master table?
thankx a lot
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
A.fruit AS bagA_fruit,
B.fruit AS bagB_fruit
FROM bags_tbl
Inner Join fruit_tbl A ON bags_tbl.bagA = A.id
Inner Join fruit_tbl B ON bags_tbl.bagB = B.id
Try this. You need to grant a unique alias to each join so SQL knows which one you're referencing in the SELECT clause.
SELECT
bags_tbl.ID,
bags_tbl.A,
bags_tbl.B,
fruitA.fruit AS bagA_fruit,
fruitB.fruit AS bagB_fruit
FROM
bags_tbl
JOIN fruit_tbl fruitA ON bags_tbl.bagA = fruitA.id
JOIN fruit_tbl fruitB ON bags_tbl.bagB = fruitB.id