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
- CONCAT the multiple sub-query results into a single field
- Have a different Line for the person's different group
Any info would be amazing :)