Linards Berzins
Linards Berzins

Reputation: 83

mysql single query for a single database table

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.

Upvotes: 0

Views: 103

Answers (1)

JYelton
JYelton

Reputation: 36546

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';

Upvotes: 2

Related Questions