How to displaying data from database [MySQL Query] - mysql

I have two tables that are related
that table is teachers and attendance
that 2 tables, is related with field teachers.id and attendance.id_teachers
now my question is, how to displaying teachers data which is that teachers data (teachers.id) is not listed in attendance table
i hope you guys understand what i'm hoping for...
I appreciate every answer
thanks

You can try with this:
SELECT * FROM teachers WHERE id NOT IN (SELECT id_teachers FROM attendance)

IN clause is your friend here. You may want to read-up on this. Following query should give the result:
select * from teachers
where id not in (select id_teachers from attendance)

Related

Get one row from table based off another

I have two tables, users and teaching
The users table contains both teachers and students. The teaching table contains the relation between the two.
I want to be able to get the student based off who "teaches" them.
Both teachers and students have an id, and I would have the teachers id to look up in the teaching table to find the students they teach.
Any ideas as to how this is done? I would like it to return the id of the student.
I've tried doing a simple join statement, but this would fetch the teacher, not the student.
Thanks in advance
Something like that?
SELECT users.id FROM users INNER JOIN teaching on users.id = teaching.userId WHERE teaching.teacherId = '';
You just need to join the users table again to the teaching table. Just make sure in one join you are using the teachers' ids and in the other the students' ids.
You can join the same table multiple times and that is totally legal in MySQL

How to count in a SQL list

I've made a table with the columns for a customer name and each activity they participate in. How can I can I count the activities for each name and display it?
I've done;
SELECT Activity_Name, COUNT(*) AS 'Number_of_activities'
FROM tablename
GROUP BY Activity_Name;
which gives me each a table of each activity and how many participants in each activity but not each customer and their number of activities
Apologies for anything I've done wrong, only a couple months into coding and first time posting on stack...
Considering I don't know how your schema looks exactly, this query should be a nice representation of the idea how to do it:
SELECT customer_name, COUNT(*) AS 'Number_of_activities_per_customer'
FROM tablename
GROUP BY customer_name;

How do I join all rows with the same name in one table using MYSQL?

Suppose I have a database called clubmembership that has a column for names, a column for clubs, and a column for the role they play in that club. The name Margrit would be in the column name many times, or as many times as she is in a club. If I want to see which people are members of the sewing club my query might look something like this:
SELECT DISTINCT NAME FROM CLUBMEMBERSHIP
WHERE CLUB=’SEWING’
AND ROLE=’MEMBER’;
My problem is that I can't figure out a query for who is not in the sewing club. Of course the simple 'not in' clause isn't working because there are plenty of rows which sewing does not appear in. In this database if someone is not in the sewing club, sewing does not appear under club so I imagine there is a way to join the different rows with the same name under 'name' and then potentially use the 'not in' clause
I hope this was a good explanation of this question. I have been struggling with this problem for a while now.
Thanks for your help!
Nicolle
This is not something that can be solved by just changing the existing code, it is to do with the database design.
Database normalisation is the process of sorting out your database into sensible tables.
If you’re adding a person many times, then you should create a table called members instead. And if there is a list of clubs, then you should create a clubs table.
Then, you can create a table to join them together.
Here’s your three tables:
members
-------
id (int)
name (varchar)
clubs
-------
id (int)
name (varchar)
memberships
-------
member_id (int)
club_id (int)
Then you can use joins in MySQL to return the information you need.
Stack Overflow doesn’t like external links as the answer should be here, but this is a huge topic that won’t fit in a single reply, so I would briefly read about database normalization, and then read about ‘joining’ tables.
If I understand you correctly, you wanted to list all names that is not a member of SEWING. The Inner query will get all Names that are member of SEWING, however, the NOT EXISTS operator will get all Names that are not found in the inner query.
SELECT DISTINCT C.NAME
FROM CLUBMEMBERSHIP C
WHERE C.ROLE = 'MEMBER'
NOT EXISTS
(
SELECT NULL
FROM CLUBMEMBERSHIP D
WHERE D.CLUB='SEWING'
AND D.ROLE='MEMBER'
AND C.NAME = D.NAME
)
Here's a Demo.

MySQL: Not to duplicate data

I have a MySQL statement that I want to use that will display the data in two different tables, but not have any duplicated data.
SELECT Customer.firstName, Customer.lastName, Purchase.productName, Purchase.productPrice
FROM Purchase
INNER JOIN Customer
This is currently the MySQL I am using and it does work, but it loads duplicated data which I do not want. I have looked around but not seeing a simple solution. Sorry in advance if it is a simple solution, been working for awhile and brain isn't really working.
You have to bind those tables via related columns.
Let's assume primary column of table Customer is named ID and customer ID is being held in a column named customerID in table Purchase:
SELECT Customer.firstName, Customer.lastName, Purchase.productName, Purchase.productPrice
FROM Purchase
INNER JOIN Customer
ON Custormer.ID=Purchase.customerID

MySQL count data from combination of two tables

I would appreciate your help with this task.
I have to use data from two tables:
tourist_country (tourist_id, country_id), and
tourist_age_category (tourist_id, age_category_id).
I know how to get number of tourists for each country id, and number of tourists for each age category. But what I need is the number of tourists for each country_id but with a specific age category.
I believe I'm close to my answer when joining those tables:
SELECT *
FROM tourist_age_category
JOIN tourist_country ON tourist_country.tourist_id = tourist_age_category.tourist_id
But it hasn't gotten me anywhere so I ask for help, thank you!
Not quite sure I understand what you mean, but perhaps this is what you want:
SELECT country_id, age_category_id, count(*)
FROM tourist_age_category
JOIN tourist_country ON tourist_country.tourist_id = tourist_age_category.tourist_id
GROUP BY country_id, age_category_id