Not sure why you include the groupMembers table in the subquery, but what about this alternative:
SELECT IF(`linktype`="group", (SELECT contactgroups.grname FROM contactgroups WHERE contactgroups.id=groupmembers.id),2) FROM groupmembers
or better yet, get rid of the subquery at all since it is not needed
SELECT IF(`linktype`="group", contactgroups.grname,2) FROM groupmembers LEFT JOIN contactgroups ON (contactgroups.id = groupmembers.id)
In addition, I have a suspicious suspicion that the table of your contact groups contains several records for one or more records in the table of your group. You can also confirm this.
source share