I am facing some tables join issue in MySQL. Can anyone help me. I have two tables in mysql database. I want to join both tables and get the records. Here is the structure of the both table.
exam_attend
===========
id
student_id
Answer
======
id
student_id
exam_attend
===========
id student_id
-- ----------
1 10
2 11
3 12
Answer
======
id student_id
-- ----------
1 10
2 10
3 13
4 12
5 14
I want list of the user who giving the answer without attending exam. Please help me.
Desired result
id student_id
-- ----------
3 13
5 14
Use:
SELECT A.id, A.student_id FROM Answer A
LEFT OUTER JOIN exam_attend E ON E.student_id=A.student_id
WHERE E.student_id IS NULL
Below SQL useful to you.
select id, student_id from Answer
where student_id not in (select distinct student_id from exam_attend)
Related
I'm defining the relationship between the two tables using a join table. I want to arrange them in the order of many overlapping things. Currently, we are using subquery, is there a way to get the same result using join?
People FoodTable PeopleFood
ID | NAME ID | Food ID | PeopleId | FoodId
1 BOB 1 Hamberger 1 1 1
2 JOHN 2 Pizza 2 1 2
3 KATY 3 Chicken 3 1 3
4 MILLER 4 Salad 4 2 1
5 AMANDA 5 Sushi 5 2 2
6 2 3
7 3 2
8 3 3
9 4 3
10 4 5
11 5 5
When the table is defined in this way, I want to arrange food tastes similar to Bob's.
I'm doing it like this now.
SELECT people_id, COUNT(people_id) as count
FROM peopleFood
WHERE food_id IN
(SELECT food_id FROM peopleFood
WHERE people_id = 1)
AND people_id != 1
GROUP BY people_id
ORDER BY count DESC;
-- Result -------------
People_id | count
2 3
3 2
4 1
Is there a better way to change this method or use join?
Thank you!!!
You have been inconsistent in your use of the table and column names -
Tables - PeopleFood in your sample data but you reference peopleFood in your query.
Columns - PeopleId and FoodId in your sample data but you reference people_id and food_id in your query.
Choose a naming convention and stick to it. Everyone has there own preference but the important thing is to be consistent.
The equivalent query with INNER JOIN instead of your sub-query is -
SELECT
`pf2`.`people_id`,
COUNT(`pf2`.`food_id`) as `count`
FROM `PeopleFood` `pf1`
INNER JOIN `PeopleFood` `pf2`
ON `pf2`.`people_id` <> `pf1`.`people_id`
AND `pf2`.`food_id` = `pf1`.`food_id`
WHERE `pf1`.`people_id` = 1
GROUP BY `pf2`.`people_id`
ORDER BY `count` DESC;
The performance difference between the two queries is unlikely to be noticeable and it might be argued that the intent is clearer in your version with the sub-query.
The surrogate key ID on your PeopleFood table should be dropped in favour of the compound “natural” primary key on people_id and food_id.
The Cost of Useless Surrogate Keys in Relationship Tables
Inner join:
SELECT p.People_id, COUNT(p.People_id) as count FROM PeopleTable p
INNER JOIN FoodTable f
ON(p.People_id = f.FoodId)
WHERE people = 1
GROUP BY p.people_id
ORDER BY count DESC;
If it helps, please mark it as an accepted answer!
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)
I have two tables:
sales Table
===============================
id cust_id total_price
===============================
1 1 1000
2 2 1500
sales_item Table
======================================================
id sales_id cust_id product quantity
======================================================
1 2 2 pen 2
2 2 2 pencil 3
3 1 1 book 2
4 1 1 pencil 2
I need to query these two tables inorder to get the following result:
=========================================
sales_id cust_id product
=========================================
2 2 pen,pencil
1 1 book,pencil
Can anyone help me to query these 2 tables inorder to get the above result??
I tried using GROUP_CONCAT. Hers is what I have tried:
SELECT s.id,s.cust_id,s.total_price,
GROUP_CONCAT(i.prdt_name)products
FROM sales s
JOIN sale_items i ON s.id=i.sales_id
And the result I got is:
======================================================
id cust_id total_price product
======================================================
2 2 1500 pen,pencil,book
This is the not the result am expecting..
Try something like this, consider that this is not tested, it may help you.
select sales.id,sales.cust_id, concat(sales_item.product)
from sales LEFT JOIN sales_item ON sales_item.sales_id = sales.id
group by sales.id
I got the answer by using GROUP_CONCAT itself. Here is my Query:
SELECT s.id,s.cust_id,s.total_price,
GROUP_CONCAT(i.prdt_name)products
FROM sales s
JOIN sale_items i ON s.id=i.sales_id
GROUP BY s.id
I have three tables having following structure
Table Name : users
id name age
1 Alok 26
2 Ashok 28
3 Amit 25
Table Name : Departments
id name d_name
1 Alok Ops
2 Amit IT
3 Shekahr CS
I want duplicate name with total count as following using mysql query
total name
2 Alok
2 Amit
1 Ashok
1 Shekhar
Please help
Thanks in Advance.
Try this:
select count(*) as total,name from (
select name from users
union
all select * from deepartment ) as temp
group by name
Union all will merge your tables and with group by and count you should get the expected result.
I am having issues figuring out how to do this. This isnt the real problem, but something very similar.
I have table A
ID Name
10 Bob
11 Tom
12 Suzie
13 Billy
14 Rob
15 Ben
Then table B, where B_ID references to ID in table A
B_ID Value
11 1500
13 2600
Then Table C where C_ID references to ID in table A
C_ID MatchedWith
10 11
12 13
14 11
15 11
The intention of this query is to list the Names of the people in table B, and how many people that are matched with them from C
...So the resulting query would give something like:
Name Count
Tom 3
Bily 1
I am completely boggled on how to do this, so any help would be super! thank you!
SELECT
A.Name,
COUNT(*) as 'Count'
FROM
C
JOIN B
ON C.MatchedWith = B.B_ID
JOIN A
ON A.ID = B.B_ID
GROUP BY A.Name
ORDER BY Count DESC;