i need to specify following in one query: It should include the Activity_name and Child_ first_name and last_name for each child registered for the specified Activity. There is (Football/Art/IT) activities in Activity Table(which has primary_key for each activity). What would be the query that would list children registered for Art activity? Children name's should be displayed in one column. Thanks.
Without providing your table structure, I cannot provide an accurate query.
However, if I were to design a set of tables that store a list of children, a list of activities, and a correlation between the two, it would consist of three tables named "children," "activities," and "linktable" in this example.
children
id
first_name
last_name
activities
id
name
linktable
child_id
activity_id
children stores the names and information of every child, and a unique id. activities stores the names and relevant information about activities, as well as a unique id. Finallly, linktable may be something like "schedule" or "signups" in your system, and should link children to activities and allow for what is called a many-to-many relationship. In other words, one child may participate in many activities, and any activity may be engaged by many children.
The following query is an example of how to see which children are signed up for Art:
SELECT
a.name, CONCAT(c.first_name, ' ', c.last_name) AS child_name
FROM
children c, activities a, linktable
WHERE
linktable.child_id = c.id
AND
linktable.activity_id = a.id
AND
a.name = 'Art';
Related
Say I have a table in a MySQL database, called people. people has many many columns, one of which is parent, which holds the primary key for another row in people. I want to do something like this:
SELECT people.*, parents.*
FROM
people
LEFT JOIN people AS parents ON people.parent = parents.id
So as to get a bunch of rows, each one of which holds the data for a person and their parent. The problem is, then I get a bunch of duplicate columns, so I can't easily refer to the child's name or the parent's name, for example.
Say there are too many columns in the people table to make listing them all out and disambiguating with AS feasible. Can something be done to automatically give different names to parents.name and people.name and so on?
I'm using SQLAlchemy in Python, so any solution that can be achieved with that tool is fine.
You could join against a select of the columns you need and give them alises:
SELECT people., parents.
FROM people
LEFT JOIN (SELECT id, col1 as colA, col2 as colb ... FROM people) AS parents
ON people.parent = parents.id
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.
Problem:
A student has mother, father, elder sister and elder brother. If the student's parents occupation is teacher, the I want to generate tree in UI by getting data from backend table. For this, I have created a table to store student information like, student id(pk), name of student, gender, address, father,mother, father_occupation, mother_occupation, etc., Also I added one option (checkbox) in UI to find student's relative occupation is teacher or not. If checkbox is true, then occupation column will be update a value true or 1.
For this, one table is enough or more than one table needed? Also how the query should if all in one table and how the query should be if need to maintain in separate tables.
You have a bunch of options for this schema, but for future scale, I would go with a hierarchy type table, and then attribute table(s) for the folks involved in the hierarchy. This would allow you to track student/teacher relationships (with the right attributes) within the same structure.
Table1 would hold your hierarchical information, which would be the relationships between your people:
id | parent_id | other_relationship_attributes
Table2 would hold information about the individual:
id | occupation | gender | etc..
You can then write queries that can traverse your hierarchy and pull relevant info from the individuals table.
If your student info is very different then your parent info (probably), then that second table would make sense to break up into two tables. 1 for your student and one for the parents.
An example of what this might look like in SQL, answering the question "Show me all students where their parents are teachers:
SELECT
student.id,
student.name,
CASE WHEN student.gender = 'F' THEN 'Daughter' ELSE 'Son' END as child_relationship
parent.id,
parent.name,
parent.occupation,
CASE WHEN parent.gender = 'M' Then 'Father' ELSE 'Mother End as parent_relationship
FROM
person as student
INNER JOIN hier on student.id = hier.id
INNER JOIN person as parent on hier.parent_id = parent.id
WHERE
parent.occupation = 'Teacher'
Considering this ER diagram
we have Students who are admitted to participate in an Exam, and each Exam can be split up into multiple Runs (for example, to split up large groups across multiple rooms or to have two Runs for the same Exam in direct succession).
Is it possible to ensure (via database constraints) that Students participate only in Runs that belong to Exams they are admitted to?
I couldn't find a way on my own and also don't know how to phrase this for an internet search.
You have these tables and columns:
exam: id, name
student: id, name
run: id, exam_id (foreign key to exam.id), when (timestamp), room
You need a new intersection table to keep track of what exam is being taken by which student:
int_exam_to_student: exam_id, student_id - both foreign keys
Now, you can query this to determine what runs a student is allowed to be in:
select run.* from run join int_exam_to_student i on (run.exam_id = i.exam_id) where i.student_id = 123;
I have 3 tables in my database: sProduct, sProductDetail and sProductDetailWarehouse. This is basically a webshop with having multiple EANs possible for a single product. For instance a t-shirt with multiple colors available, each color being it's own EAN.
The important bits about tables:
sProduct has ID which is primary key and title (varchar).
sProductDetail has ID (primary key), ID_sProduct (the correlation to the sProduct table), EAN and title
sProductDetailWarehouse has ID (primary key), ID_sProductDetail (correlation to the detail table) and stock (int).
What I would want is to use something similar to this:
select pd.ID,pd.title,pdw.stock from sProduct p
inner join sProductDetail pd on pd.ID_sProduct=p.ID
left join sProductDetailWarehouse pdw on pdw.ID_sProductDetail=pd.ID
and only have it return 1 record on join with the highest stock. The problem is I can't use order by since I have multiple products in a query needing to be ordered by their release date.
So basically out of every one sProduct.ID I would need only one sProductDetail.ID returned even though there might be many. Can anyone help with this?
Thanks.