student table
|----------------------|
| student_id | name |
|------------|---------|
| 1 | Richard |
| 2 | Emily |
| 3 | Hans |
|------------|---------|
lecturer table
|--------------------|
| lecturer_id | name |
|-------------|------|
| 1 | John |
| 2 | Mike |
|-------------|------|
classes table
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 1 | 1 | Basic of algorithm |
| 2 | 1 | Basic of programming |
| 3 | 2 | Database Essentials |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
attendance table
|-----------------------|
| class_id | student_id |
|----------|------------|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
| 4 | 1 |
| 4 | 2 |
|----------|------------|
how to show classes records (from classes table) that not attended by Hans (student) in MySQL?
desired result :
|-----------------------------------------------|
| class_id | lecturer_id | material |
|----------|-------------|----------------------|
| 2 | 1 | Basic of programming |
| 4 | 2 | Basic of SQL |
|----------|-------------|----------------------|
One approach uses EXISTS:
SELECT c.class_id, c.lecturer_id, c.material
FROM classes c
WHERE NOT EXISTS (SELECT 1 FROM attendance a
INNER JOIN student s
ON a.student_id = s.student_id
WHERE a.class_id = c.class_id AND
s.name = 'Hans');
Using joins -
select c.class_id
from attendance a inner join student s on (a.student_id=s.student_id and s.student_id='Hans')
right outer join classes c on (a.class_id=c.class_id)
where a.class_id is null
Related
I have two tables as follows.
Category table
+------------+------------+--------------+
| CategoryID | Name | CategoryCode |
+------------+------------+--------------+
| 1 | Fixed | |
| 2 | Consumable | |
| 3 | Intangible | |
+------------+------------+--------------+
Type table
+--------+------------+-------------------------+----------+
| TypeID | CategoryID | Name | TypeCode |
+--------+------------+-------------------------+----------+
| 1 | 1 | Furniture | |
| 2 | 1 | Computers & Peripherals | |
| 3 | 2 | Keyboards | |
| 4 | 2 | Other | |
| 5 | 3 | Software | |
+--------+------------+-------------------------+----------+
The result I want is like this
+------------+------------+------------------------------------+
| CategoryID | Category | Types |
+------------+------------+------------------------------------+
| 1 | Fixed | Furniture, Computers & Peripherals |
| 2 | Consumable | Keyboards, other |
| 3 | Intangible | Software |
+------------+------------+------------------------------------+
Appriciate if you could help me with wirting the query in MySQL
You can try this solution
SELECT
a.CategoryId,
a.`Name` Category,
GROUP_CONCAT(b.`Name`) Types
FROM
Category a
INNER JOIN Type b ON b.CategoryID = a.CategoryId
GROUP BY a.CategoryId
I have 4 tables: the first is the client table, which has customer info, and client_id as an auto-increment primary key.
The second and third are identical in structure: they are used to track attendance to 2 different therapy programs. They each have a primary key, and a client_id column to track the client. One of the fields contains units, which I want to sum.
The last table contains the therapists' info.
Basically I want to extract total amount of units for each client from the two attendance tables.
I have tried LEFT JOINS to no avail. I also tried a UNION ALL, but couldn't get it to sum the units.
This is how the tables look:
client:
+---------------------------------------+
| client_id | f_name | l_name | th_id |
|-----------|----------|--------|-------|
| 1 | sherlock | holmes | 1 |
| 2 | john | watson | 4 |
| 3 | hercule | poirot | 3 |
| 4 | jane | marple | 2 |
+---------------------------------------+
therapist:
+--------------------------+
| th_id | f_name | l_name |
|-------|---------|--------|
| 1 | james | kirk |
| 2 | mr | spock |
| 3 | bones | mccoy |
| 4 | nyota | uhura |
+--------------------------+
attendance it:
+-------------------------------+
| it_id | client_id | units |
|-----------|-----------|-------|
| 1 | 1 | 4 |
| 2 | 1 | 4 |
| 3 | 1 | 0 |
| 4 | 1 | 2 |
| 5 | 4 | 0 |
| 6 | 4 | 4 |
| 7 | 4 | 0 |
| 8 | 4 | 2 |
+-------------------------------+
attendance psr:
+-------------------------------+
| it_id | client_id | units |
|-----------|-----------|-------|
| 1 | 1 | 16 |
| 2 | 1 | 16 |
| 3 | 1 | 0 |
| 4 | 1 | 12 |
| 5 | 4 | 0 |
| 6 | 4 | 14 |
| 7 | 4 | 8 |
| 8 | 4 | 10 |
+-------------------------------+
The result should look like this:
+------------------------------------------------------------+
| client_id | total_units_it | total_units_psr | therapist |
|-----------|----------------|-----------------|-------------|
| 1 | 10 | 44 | james kirk |
| 4 | 6 | 32 | mr spock |
+------------------------------------------------------------+
Please excuse the primitive representations, and please don't ask why the tables are designed like that... ;-) Also, I obviously ignored many other fields which are not relevant to the question, such as dates, etc.
Any advice would be appreciated.
Thanks!
You can't use join or you will create Cartesian product and duplicate the rows.
Instead you do a subquery:
SELECT c.*
, (SELECT SUM(units) FROM attendance_it a WHERE a.client_id = c.client_id ) as total_units_it
, (SELECT SUM(units) FROM attendance psr a WHERE a.client_id = c.client_id ) as total_units_psr
, t.*
FROM client c
JOIN therapist t
ON c.th_id = t.th_id
use group by client_id to get the sum of each client. and no need to use join as you have already the ids in column.
I have three tables college_details, college_courses, course of which college_courses is an intermediate table for many to many relation ships between college_details and course.
college_details
+------------+--------------+
| college_id | college_name |
+------------+--------------+
| 1 | abc, Nagpur |
| 2 | xyz, Nagpur |
| 3 | mnop, Nagpur |
+------------+--------------+
college_courses
+------------+--------------+
| college_id | course_id |
+------------+--------------+
| 1 | 1 |
| 1 | 5 |
| 2 | 3 |
| 3 | 5 |
+---------------------------+
course
+----------+-------------+
|course_id | course_name |
+----------+-------------+
| 1 | B.E |
| 2 | M.E |
| 3 | M.Tech |
| 4 | B.Tech |
| 5 | M.B.A |
+----------+-------------+
SELECT cd.college_id
, cd.college_name
, c.course_name
FROM college_details AS cd
LEFT
JOIN college_courses AS cc
ON cc.college_id = cd.college_id
LEFT
JOIN course AS c
ON cc.course_id = c.course_id
From the above query I get the following table.
+------------+---------------+-------------+
| college_id | college_name | course_name |
+------------+---------------+-------------+
| 1 | abc, college | be |
| 1 | abc, college | mba |
| 2 | xyz, college | me |
| 3 | mnop, college | btech |
+------------+---------------+-------------+
query -> "SELECT cd.college_id,cd.college_name,GROUP_CONCAT(c.course_name) FROM college_details AS cd LEFT JOIN college_courses AS cc ON cc.college_id = cd.college_id LEFT JOIN course AS c ON cc.college_id = c.course_id ";
And from above query I get the following table.
+------------+--------------+-----------------------------+
| college_id | college_name | GROUP_CONCAT(c.course_name) |
+------------+--------------+-----------------------------+
| 1 | abc, college | be,be,me,btech |
+------------+--------------+-----------------------------+
But what I want is the following table.
+------------+---------------+-------------+
| college_id | college_name | course_name |
+------------+---------------+-------------+
| 1 | abc, college | be,mba |
| 2 | xyz, college | me |
| 3 | mnop, college | btech |
+------------+---------------+-------------+
I have searched and gone through the available topics similar to mine. But, failed to find that satisfies my requirements. Hence, posting it here.
I have four tables as follows:
"Organization" table:
--------------------------------
| org_id | org_name |
| 1 | A |
| 2 | B |
| 3 | C |
"Members" table:
----------------------------------------------
| mem_id | mem_name | org_id |
| 1 | mem1 | 1 |
| 2 | mem2 | 1 |
| 3 | mem3 | 2 |
| 4 | mem4 | 3 |
"Resource" table:
--------------------------------
| res_id | res_name |
| 1 | resource1 |
| 2 | resource2 |
| 3 | resource3 |
| 4 | resource4 |
"member-resource" table:
--------------------------------------------
| sl_no | mem_id | res_id |
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 4 | 3 |
| 5 | 3 | 4 |
| 6 | 2 | 3 |
| 7 | 4 | 3 |
I want to find out the total number of distinct resources according to organizations. Expected output is as follows:
| org_name | Total Resources |
| A | 3 |
| B | 1 |
| C | 1 |
I also want to find out the total number of shared resources according to organizations. Expected output is as follows:
| org_name | Shared Resources |
| A | 1 |
| B | 0 |
| C | 1 |
Any help in this regard will highly be appreciated.
Regards.
It is much simpler than you think, particularly because you don't even need the resource table:
SELECT o.org_name, COUNT(DISTINCT mr.res_id) TotalResources
FROM member_resource mr
JOIN members m ON mr.mem_id = m.mem_id
JOIN organization o ON m.org_id = o.org_id
GROUP BY o.org_id
Output:
| ORG_NAME | TOTALRESOURCES |
|----------|----------------|
| A | 3 |
| B | 1 |
| C | 1 |
Fiddle here.
Try this query below.
SELECT org_name, COUNT(DISTINCT res_id)
FROM organization, members, member-resource
WHERE members.mem_id = member-resource.mem_id
AND organization.org_id = members.org_id
GROUP BY org_id, org_name
I need help for generating a SQL for MySQL database.
I have two tables:
Students
student_Subjects
Students table:
+------------+---------+-----------------+
| id | name |area_of_study_id |
+------------+---------+-----------------+
| 1 | AAA | 1 |
| 2 | BBB | 2 |
| 3 | CCC | 1 |
| 4 | DDD | 3 |
| 5 | EEE | 4 |
| 6 | FFF | 1 |
| 7 | GGG | 2 |
| 8 | III | 1 |
+------------+---------+-----------------+
student_subjects table:
+------------+-------------------+------------------+
| id | student_id | subject_id |
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 3 |
| 4 | 1 | 2 |
| 5 | 3 | 1 |
| 6 | 6 | 1 |
| 7 | 7 | 3 |
| 8 | 4 | 6 |
+------------+-------------------+------------------+
I need to find out a subject's area of study based on students area of study. From above students table you can see that students have the freedom to choose the area of study. Sometimes, they can choose subjects which are not related to their area of study. So, in this case, we need to calculate area of study for a subject based on the student percentages who have taken the subjects.
Partial output:
+------------+-------------------+-----------------------------------+
|Total_students | subject_id |area_of_study_id | percentage |
+------------+-------------------+-----------------+-----------------+
| 3 | 1 | 1 | 75 |
| 1 | 1 | 2 | 25 |
| 1 | 3 | 1 | 50 |
| 1 | 3 | 2 | 50 |
| 1 | 2 | 1 | 100 |
| 1 | 6 | 3 | 100 |
+------------+-------------------+-----------------+-----------------+
From above partial output, we can see, 3 students choose a subject(i.e subject_id =1) have the area of study 1 and one student is from different area of study (i.e 2). So, we can say subject_id =1 is from area of study = 1.
if percentage 50% for a subject. we can choose any area of study between two, no matter which one.
Expected output:
+------------+-------------------+-----------------------------------+
|Total_students | subject_id |area_of_study_id | percentage |
+------------+-------------------+-----------------+-----------------+
| 3 | 1 | 1 | 75 |
| 1 | 3 | 1 | 50 |
| 1 | 2 | 1 | 100 |
| 1 | 6 | 3 | 100 |
+------------+-------------------+-----------------+-----------------+
Try it this way
SELECT subject_id, area_of_study_id, total_students, percentage
FROM
(
SELECT p.subject_id, p.area_of_study_id, p.total_students,
p.total_students / t.total_students * 100 percentage
FROM
(
SELECT COUNT(*) total_students, ss.subject_id, s.area_of_study_id
FROM students s JOIN student_subjects ss
ON s.id = ss.student_id
GROUP BY ss.subject_id, s.area_of_study_id
) p JOIN
(
SELECT ss.subject_id, COUNT(*) total_students
FROM students s JOIN student_subjects ss
ON s.id = ss.student_id
GROUP BY ss.subject_id
) t ON p.subject_id = t.subject_id
ORDER BY percentage DESC
) q
GROUP BY subject_id;
Output:
| SUBJECT_ID | AREA_OF_STUDY_ID | TOTAL_STUDENTS | PERCENTAGE |
|------------|------------------|----------------|------------|
| 1 | 1 | 3 | 75 |
| 2 | 1 | 1 | 100 |
| 3 | 1 | 1 | 50 |
| 6 | 3 | 1 | 100 |
Here is SQLFiddle demo