Kyle.Belanger Kyle.Belanger - 2 months ago 10
MySQL Question

Combining select queries

I am working with a MySQL database. I am suppose to combine three select queries, to "improve performance". Each select below is dependent on the previous ID retrieved.

So far, I've tried the following...

# multiple select from tables
select user.name, group.ID
from user as u, group as g
where u.name = <name_here>

# inner join...
select user.ID, group.ID,
from user
inner join group
on user.ID = group.ID


I need to select the user.name and group.ID based on a username param. Is there a way to query this data in a single statement?

Answer

I don't know if I understand your need, lets try:

Try to use this query:

select pGroupMatch.GroupID, ProfileData.ID
from pUserMatch
inner join pGroupMatch on pGroupMatch.GroupID = pUserMatch.GroupID 
inner join ProfileData on ProfileData.id = pGroupMatch.ProfileID 
where pUserMatch.username = "<username>";

Check if you can create indexes for improve your query, if you can try it:

CREATE INDEX idx_pUserMatch_01 ON pUserMatch (GroupID);
CREATE INDEX idx_pGroupMatch_01 ON pGroupMatch (ProfileID);