Group_concat use?

carlgoodtoseeyou

I have three tables in my DB, Student, Participant, and Activity. I'm trying to pull ALL the student names, activity names, and activity costs for all students participating in a certain activity. I'm wondering if this is possible. Here is the DB if you want to take a look: http://pastebin.com/QCsQfEKF

Basically I want a table that looks like this: (ex. data)

Name  /         Activities        /Activities Cost
carl  /basketball, golf           /$500
jane  /pottery, skydiving, golf   /$600

and I want the data selected by giving a certain activty that they all participate in.

I know how to do all of it except for the, selecting those who are all in a certain activity, part.

Michael Berkowski

You'll need several joins. Most importantly, to get the activity that they all participate in, you may use a separate joined alias (join more than once against participants and activities). One of the aliased joins is limited in the WHERE clause to only those having the desired activity, to return a list of student ids. Then you use that list to form the rest of your query.

SELECT
  s.stu_fname,
  s.stu_lname,
  GROUP_CONCAT(a.act_name) AS Activities,
  SUM(a.act_fee) AS `Activities Cost`
FROM
  /* These first two joins doing go into the SELECT list.
     They are used to get the list of students with your desired activity
     and will be used in the WHERE clause */
  participant plimit
  INNER JOIN activity alimit ON plimit.act_id = alimit.act_id
  /* Only include those students from the first 2 joins */
  INNER JOIN student s ON plimit.stu_id = s.stu_id
  /* Then join through the participants to get all activities for those students */
  INNER JOIN participant p ON s.stu_id = p.stu_id
  /* And to get the names of the activities */
  INNER JOIN activity a ON p.act_id = a.act_id
/* Limit to only those students with golf */
WHERE alimit.act_name = 'pottery'
/* Apply the aggregate GROUP BY to the rest of the columns in the SELECT */
GROUP BY 
  s.stu_id,
  s.stu_fname,
  s.stu_lname

Here it is in action: http://sqlfiddle.com/#!2/37860/6

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reasons not to use GROUP_CONCAT?

From Dev

How can I use GROUP_CONCAT in Rails?

From Dev

Invalid use of group function (group_concat and MySQL)

From Dev

MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

From Dev

GROUP_CONCAT with limit

From Dev

How to use group_concat in hibernate criteria?

From Dev

How to use sum and joins within group_concat

From Dev

MySQL Use GROUP_CONCAT with Multiple JOINS

From Dev

how to use GROUP_CONCAT of two columns with query

From Dev

Sorting in group_concat

From Dev

How to use GROUP_CONCAT in mySQL when one of the fields contain comma seperated numbers?

From Dev

How to use conditional statement in conjunction with group_concat?

From Dev

Use GROUP_CONCAT query in Rails

From Dev

MySQL use GROUP_CONCAT when INSERTing data

From Dev

Is it possible to use group_concat with coredata?

From Dev

Group_concat use?

From Dev

MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

From Dev

How to use GROUP_CONCAT with MAX(CASE WHEN...)?

From Dev

How do I use JPA 2.1's CriteriaBuilder.function with MySQL's "GROUP_CONCAT"?

From Dev

How to use sum and joins within group_concat

From Dev

how to use group_concat for some data like this?

From Dev

How to fetch details and how to connect category and book table structure ( how to use GROUP_CONCAT)

From Dev

MySQL: update with join using GROUP_CONCAT: ERROR 1111 (HY000): Invalid use of group function

From Dev

How to COUNT MySQL results when I use “GROUP_CONCAT” and “HAVING” in the query?

From Dev

Query is not executing properly when I use GROUP_CONCAT with in keyward

From Dev

Use conditions in GROUP_CONCAT

From Dev

how to use select query in a Group_concat sub query in mysql

From Dev

MySQL - is it possible to use group_concat in an IN() statement?

From Dev

How can I use group_concat on an entire subquery?

Related Related

  1. 1

    Reasons not to use GROUP_CONCAT?

  2. 2

    How can I use GROUP_CONCAT in Rails?

  3. 3

    Invalid use of group function (group_concat and MySQL)

  4. 4

    MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

  5. 5

    GROUP_CONCAT with limit

  6. 6

    How to use group_concat in hibernate criteria?

  7. 7

    How to use sum and joins within group_concat

  8. 8

    MySQL Use GROUP_CONCAT with Multiple JOINS

  9. 9

    how to use GROUP_CONCAT of two columns with query

  10. 10

    Sorting in group_concat

  11. 11

    How to use GROUP_CONCAT in mySQL when one of the fields contain comma seperated numbers?

  12. 12

    How to use conditional statement in conjunction with group_concat?

  13. 13

    Use GROUP_CONCAT query in Rails

  14. 14

    MySQL use GROUP_CONCAT when INSERTing data

  15. 15

    Is it possible to use group_concat with coredata?

  16. 16

    Group_concat use?

  17. 17

    MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

  18. 18

    How to use GROUP_CONCAT with MAX(CASE WHEN...)?

  19. 19

    How do I use JPA 2.1's CriteriaBuilder.function with MySQL's "GROUP_CONCAT"?

  20. 20

    How to use sum and joins within group_concat

  21. 21

    how to use group_concat for some data like this?

  22. 22

    How to fetch details and how to connect category and book table structure ( how to use GROUP_CONCAT)

  23. 23

    MySQL: update with join using GROUP_CONCAT: ERROR 1111 (HY000): Invalid use of group function

  24. 24

    How to COUNT MySQL results when I use “GROUP_CONCAT” and “HAVING” in the query?

  25. 25

    Query is not executing properly when I use GROUP_CONCAT with in keyward

  26. 26

    Use conditions in GROUP_CONCAT

  27. 27

    how to use select query in a Group_concat sub query in mysql

  28. 28

    MySQL - is it possible to use group_concat in an IN() statement?

  29. 29

    How can I use group_concat on an entire subquery?

HotTag

Archive