Question

I know that there are lots of these questions, but I was unable to find a definitive answer elsewhere.

I have 4 tables:

  • person_per
  • person2group2role_p2g2r
  • group_grp
  • list_lst

I am wanting to run a query which shows all the information about a person, including the group types they are in

The per_ID is in the person_per table

The many-to-many bridge has the per_ID, grp_ID and role_ID

the grp_ID is in the group_grp table

the grp_type in the group_grp table links with the lst_optionID

the lst_OptionName is the name in text (what i finally want displayed)

I tried doing it in two ways, hitting the same error in both. Option 1 uses WHERE args to link tables whereas Option 2 uses JOIN statements. This is all in the second subquery. The main query is pulling personal information and the first subquery is pulling the classification of the person.

Option 1:

SELECT person_per.per_ID as AddToCart, CONCAT(person_per.per_FirstName, ' ', person_per.per_MiddleName) AS 'FirstNames', person_per.per_LastName AS 'Surname', 
    (SELECT lst_OptionName FROM list_lst WHERE list_lst.lst_ID=1 AND
        list_lst.lst_OptionID=person_per.per_cls_ID) AS "Classification",
    person_per.per_HomePhone AS 'Tel', person_per.per_WorkPhone AS 'Cell',
    person_per.per_Email AS 'Email',
    --START DIFF
    (SELECT lst_OptionName 
    FROM list_lst, person2group2role_p2g2r, group_grp, person_per 
    WHERE list_lst.lst_ID=3 
        AND person_per.per_ID = person2group2role_p2g2r.p2g2r_per_ID 
        AND person2group2role_p2g2r.p2g2r_grp_ID = group_grp.grp_ID 
        AND group_grp.grp_Type = list_lst.lst_ID) AS "LifeGroupType"
    --END DIFF
FROM person_per
ORDER BY person_per.per_LastName

Option 2:

SELECT person_per.per_ID as AddToCart, CONCAT(person_per.per_FirstName, ' ', person_per.per_MiddleName) AS 'FirstNames', person_per.per_LastName AS 'Surname',
    (SELECT lst_OptionName FROM list_lst WHERE list_lst.lst_ID=1 AND
        list_lst.lst_OptionID=person_per.per_cls_ID) AS "Classification",
    person_per.per_HomePhone AS 'Tel', person_per.per_WorkPhone AS 'Cell',
    person_per.per_Email AS 'Email',
    --START DIFF
    (SELECT lst_OptionName 
    FROM list_lst 
        JOIN group_grp ON group_grp.grp_Type = list_lst.lst_ID
        JOIN person2group2role_p2g2r ON person2group2role_p2g2r.p2g2r_grp_ID = group_grp.grp_ID
        JOIN person_per ON person_per.per_ID = person2group2role_p2g2r.p2g2r_per_ID
    WHERE list_lst.lst_ID=3) AS "LifeGroupType"
    --END DIFF
FROM person_per
ORDER BY person_per.per_LastName

I understand that this is an issue because each person can be assigned to multiple groups, I was just wondering how I can split the different groups up. Preferable outcome would be a single row per person with all their info and the groups they are in. Otherwise how would I

  1. CONCAT the multiple sub-query results into a single field
  2. Have a different Line for the person's different group

Any info would be amazing :)

Was it helpful?

Solution

I am going to assume the problem is that the subqueries are returning more than one row, because people have more than one item in each table. Then, I'm going to assume that you are using MySQL. The solution is a function called group_concat():

SELECT p.per_ID as AddToCart, CONCAT(p.per_FirstName, ' ', p.per_MiddleName) AS FirstNames, 
       p.per_LastName AS Surname, 
       (SELECT group_concat(lst_OptionName)
        FROM list_lst l
        WHERE l.lst_ID = 1 AND l.lst_OptionID = p.per_cls_ID
       ) AS Classification,
       p.per_HomePhone AS Tel, p.per_WorkPhone AS Cell, p.per_Email AS Email,
    --START DIFF
       (SELECT lst_OptionName 
        FROM person2group2role_p2g2r p2g join
             group_grp g
             on p2g.p2g2r_grp_ID = g.grp_ID join
             list_lst l
             on g.grp_Type = l.lst_ID
        WHERE l.lst_ID = 3 and p.per_ID = p2g.p2g2r_per_ID
       ) AS LifeGroupType
    --END DIFF
FROM person_per p
ORDER BY p.per_LastName;

Along the way, I also added table aliases to make the query more readable, explicit join syntax, and removed the extra unnecessary reference to person_per in the subquery. That might have been the cause of your problem in the first place.

OTHER TIPS

you are selecting many values in subquery while should be just one.

try use LIMIT 1 inside subquery will solve it. or you have to select value what you need but it must be one value.

  SELECT person_per.per_ID as AddToCart         ,       (SELECT .......)
            ^^^^^^^^^^^^^--- you selecting 1value here     ^^^^^^
                                                         |____you should select here also one value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top