I have a simple dummy sql with my dummy head:(
select if(`linktype`="group", (select contactgroups.grname
开发者_开发百科 from contactgroups, groupmembers
on contactgroups.id=groupmembers.id),2)
from groupmembers
this should list only the groups name but I can't see what I've done wrong again:( Any help, please?
Another try:
SELECT contactgroups.grname
FROM contactgroups, groupmembers
WHERE contactgroups.id = groupmembers.id
works well gives 2 group names back. So this is why the error message. But if I select from groupmembers it should match with the groupmembers.group_id with the contactgroups.id
if
must return a scalar value.
Never tested it but try to substitute contactgroups.grname
with group_concat(contactgroups.grname)
Not sure why you are including the groupMembers table in the subquery, but how 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 altogether as it is not needed
SELECT IF(`linktype`="group", contactgroups.grname,2)
FROM groupmembers
LEFT JOIN contactgroups ON (contactgroups.id = groupmembers.id)
Also, I have a sneaking suspicion that your contactgroups table has multiple entries for one or more of your groupmembers table entries. You might want to confirm that too.
The nested select
(select contactgroups.grname on contactgroups.id=groupmembers.id)
seems to return more than 1 row, You could use LIMIT
to restrict this. Or if it's not expected, check your data.
精彩评论