How to count rows in mysql subquery - mysql

I have two tables course and registered_course
course table
crid
crname
crlevel
1
math
senior
2
english
senior
3
physics
senior
Registered course table
id
crid
student_id
1
2
25
2
2
26
3
3
23
4
3
24
5
3
27
so i want to achieved this result the first table join with second table and a count of students that registered a subject just like below thanks
crname
crlevel
number_of_student
math
senior
2
physic
senior
3

SELECT table1.crname, table1.crlevel, count(table2.studentid)
FROM table1
INNER JOIN table2 ON table1.crid = table2.crid
GROUP BY table1.crname, table1.crlevel

Related

Joining 5 tables with parent child relation in SQL

I am working on a query with a join of 5 tables, and also there is parent child relation.
I am trying to query university sales and this is my query. I am struggling to get total sales of university. How should I query this?
I attached category table and invoice table.
Select invoice.id, invoice.total, categories.type, categories.name
FROM invoices
Join sales on sales.invoice_id = invoices.id
Join course on course.id = sales.course_id
Join category_subject on category_subject.subject_id = course.subject_id
Join categories on categories.id = category_subject.category_id
categories table with parent-child relationship.
Here is the categories table:
id
parent_id
name
type
1
null
England
country
2
null
France
country
3
16
A university
university
4
17
B university
university
5
16
C university
university
6
17
D university
university
7
1
E high school
stage
8
2
F high school
stage
9
3
computer engineering
department
10
4
art
department
11
5
chemistry
department
12
7
grade 9
grade
13
7
grade 10
grade
14
8
grade 11
grade
15
8
grade 12
grade
16
1
England- university
stage
17
2
France-university
stage
18
6
business
department
and this is invoice table:
id
total
1
50
2
100
3
350
4
850
5
65
6
75
7
850
8
650
9
250
10
450
11
300
12
100
13
450
14
950
15
350
16
750
17
320
There's not currently enough information included the in the question to really answer it, but start with this and tell us what you're still missing:
SELECT i.id, i.total, ct.type, ct.name
FROM invoices i
INNER JOIN sales s on s.invoice_id = i.id
INNER JOIN course c on c.id = s.course_id
INNER JOIN category_subject cs on cs.subject_id = c.subject_id
INNER JOIN categories ct on ct.id = cs.category_id
WHERE ct.type = 'University'
Additionally, from what I've inferred you want include all invoices for category IDs 3, 4, 5, and 6 -- which are directly Universities -- as well as 9, 10, 11, and 18 -- which are children of the Universities.
This is easy enough to do if you know you only have one level like this. One additional join + a coalesce() in the WHERE clause can do the job, as could a UNION to a similar query. But if you could have arbitrarily more levels the query becomes much trickier; you must use a recursive CTE to check the whole category tree. Also, right now the sample data never shows a University with a parent category. If the actual data allows for this that adds another wrinkle. So again: not enough information in the question to provide a good answer at this time.

need output with sub query from the tables in mysql

FROM THE GIVEN TABLES: Teacher And Student
teacher_ID name city fee
------------------- ------------------------
1 Jit New York 15000
2 Nilon Paris 13000
5 Pol London 11000
6 Maj Paris 14000
7 Paul Rome 13000
3 Liza Saudi 12000
----------------------------------------------------------
student_id name city rank teacher_ID
2 Nick New York 1 1
7 Brady New York 2 1
5 Gunman California 2 2
8 Juli London 3 2
4 Faral Paris 3 6
9 Goku Berlin 1 3
3 John Moscow 2 7
1 Badstar London 5
From the Given Tables I need to make a list in ascending order for the students who holds a rank less than 3 and taught either by a teacher or by own.
It's expected output is this
name city rank Teacher city
Nick New York 1 Jit New York
John Moscow 2 Paul Rome
Gunman California 2 Nilon Paris
Brady New York 2 Jit New York
Goku Berlin 1 Liza Saudi
I tried writing sql code
SELECT name,city,rank FROM Student WHERE rank < 3 ORDER BY student_ID ASC;
That code gives me first 3 columns of the expected output however I've no idea how to get the last two column of the expected output, any suggestions are welcome
The query...
SELECT Student.name,
Student.city,
rank,
Teacher.name as "Teacher",
Teacher.city
FROM Student
LEFT JOIN Teacher on Teacher.teacher_ID = Student.teacher_ID
WHERE rank < 3
ORDER BY student_ID ASC;
There are a few things that you should consider in the statement
When selecting the fields you want to display you need to make sure table column references are unique. As both tables have the field name and city, you should specifically reference them using the format of [table].[column name]
There are multiple ways that you can join tables. In this case, you want to use a left join because all data exists in the tables on the left side of the join, and might not exist in the table on the right side of the join.
If the student has a result of less than 3 and no teacher, the teacher name and city will appear as null. You might want to consider adding an IFNULL or similar to make these fields clear.

How to check if a non-string value exists in a record set generated from GROUP?

I have 3 tables,
-- table of selected_courses
student_id course_id
1 11
1 12
1 13
2 11
2 12
3 12
3 13
4 11
-- table of students
student_id name
1 Adam
2 Bill
3 Calvin
4 David
-- table of courses
course_id name
11 math
12 physics
13 chemistry
I would like to find those students who selected both physics(12) and chemistry(13), generally, in this case they should be Adam and Calvin.
Generally, I can get each student's course records by grouping
select * from selected_courses group by student_id;
then how can I find out if both 12 and 13 are in the student's group?
BTW, I am using mysql.
use aggregation
select student_id from
selected_courses a
where course_id in (12,13)
group by student_id
having count(distinct course_id)=2
Use correlated subquery
select student_id, course_id
from tablename a where exists
(select 1 from tablename b where a.student_id=b.student_id and course_id in (12,13) having count(distinct course_id)=2)

mysql select in another select group: how many people in downline?

Hello i've a table similar to this one:
id sponsor name
------------------------
1 0 Sasha
2 1 John
3 1 Walter
4 3 Ashley
5 1 Mark
6 4 Alexa
7 3 Robert
8 3 Frank
9 4 Marika
10 5 Philip
11 9 Elizabeth
when i choose an ID (call it MYCHOICE) i want know all the name of people who has sponsor like MYCHOICE... is simply:
select * from tablename where sponsor=MYCHOICE
but... here is the problem... i would know how many people there is in the downline of this results... so... how many records there are with sponsor like each id.
if i choose id 1 result should be
id name downline
----------------------
2 John 0 (noone with sponsor=2)
3 Walter 3 (3 with sponsor=3: ashley, robert, frank)
5 Mark 1 (1 with sponsor=5: philip)
if i choose id 4 result should be
id name downline
----------------------
6 Alexa 0
9 Marika 1 (1 with sponsor=9: Elizabeth)
i try this "bad solution" if mychoice is 1
select sponsor,count(*) as downline from tablename where sponsor in
(select id from tablename where sponsor=1) group by sponsor order by
downline desc
result of this query is
sponsor downline
---------------------
3 3
5 1
there are 2 problems:
- names are not rights and is not that i want
- the count 0 "2|John|0" in the example dont appears
thank u for advice and help, sorry for english,
N.
SELECT child.id,
child.name,
COUNT(grandchild.sponsor) downline
FROM TableName child
INNER JOIN TableName parent
ON child.sponsor = parent.id AND
parent.id = ? -- << user choice
LEFT JOIN TableName grandchild
ON child.id = grandchild.sponsor
GROUP BY child.id, child.name
SQLFiddle Demo
As you can see, the table is joined to itself twice. The first join that uses INNER JOIN gets the records associated with the Sponsor which is your user_choice. The second join which uses LEFT JOIN gets all the records associated with records from your user_choice.

What to use in order to have full consolidated table in Mysql [duplicate]

This question already has answers here:
Join of two tables as to get entire records
(2 answers)
Closed 8 years ago.
I am creating two tables .
Table 1 has following schema
user_id int not null,autoincrement
movie _id int not null
movie_name varchar
user_name varchar
rating int
genre varchar
while table 2 has following schema
movie _id int not null
movie_name varchar
user_name varchar
genre varchar
rating varchar
Now when I put a query to insert values it first checks whether a following username exist in first table .If true then it inserts into second table otherwise it inserts value as per schema into first table.In other words first table has unique usernames and unique user_Id while second contains many repeated usernames with movie they have seen
So there are following values which I am inserting via form(Java ,servlet) into my tables
user_Id movie_Id movie_name user_name rating genre
1 1 Twister Alex 6 Drama
2 1 Twister Tim 1 Drama
3 2 sweet november pam 5 Romantic
4 3 The pianist carl 5 Drama
5 4 narnia stephen 7 Fantasy
..
..
(contd..)
Table 2
movie_Id movie_Name user_name genre Rating
2 sweet november Alex Romantic 4
3 The pianist Alex Drama 5
4 narnia Pam Fantasy 8
9 maceth Tim Drama 9
..
....
(contd.)
..
.Further I want to merge both the tables so that it gives me the following image
user_id movie_Id movie_name user_name rating genre
1 1 Twister Alex 6 Drama
1 2 sweet november Alex 4 Romantic
1 3 The pianist Alex 5 Drama
2 1 Twister Tim 1 Drama
2 9 macbeth Tim 9 Drama
3 2 Sweet November Pam 5 Romatic
3 4 Narnia Pam 8 Fantasy
4 3 The Pianist Carl 5 Drama
5 4 Narnia Stephen 7 Fantasy
... and so on
What should I use
I tried with join but it neglects first table value.I want to have both table values just after i enter values in form and then i click
This is following syntax which i was using
select * from table2 inner join table1 on table2.user_name = table1.user_name
Please suggest something
Thanks
You have a really bad data design. For instance, there is no obvious link from table2 to table1. It should have a user_id column rather than user_name.
The answer to your question, though, is union all rather than a join (or, rather, in addition to). You need a join to look up the user_id for the second table:
select user_id, movie_Id, movie_name, user_name, rating, genre
from Table1 union all
select t1.user_id, t2.movie_Id, t2.movie_name, t2.user_name, t2.rating, t2.genre
from table2 t2 join
table1 t1
on t2.user_name = t1.user_name;
That said, you should revise your database structure. As a hint, it should have three tables: users, movies, and user_movies.