I have a query that looks right to me but MySQL is giving me a syntax error.
Code:
SELECT ObjectID FROM ObjectGroups WHERE GroupID IN
((SELECT GroupID FROM Groups WHERE GroupOwner='sally')
UNION
(SELECT Groups.GroupID AS GroupID FROM GroupMembers
JOIN Groups ON GroupMembers.GroupID=Groups.GroupID
WHERE GroupMembers.Member='sally'))
The UNION of the subquery works by itself. The query with IN works with either half of the query. But fails when the UNION is the subquery.
DETAILS
Four tables. Objects, Groups, GroupMembers, and ObjectGroups.
Groups are groups of users. Users can be in more than one group. If you "Own" a group, you are automatically a member of that group (i.e., not on the groupmember table).
Objects can be assigned to an arbitrary number of groups. Thus, the ObjectGroups table.
Here are the tables:
Code:
Create Table Groups
(
GroupID int not null auto_increment primary key,
GroupName varchar(100) not null,
GroupOwner varchar(22) not NULL
);
Create Table GroupMembers
(
GroupID int unsigned not NULL,
Member varchar(22) not NULL,
PRIMARY KEY (GroupID, Member)
);
Create Table ObjectGroups
(
ObjectID int unsigned not NULL,
GroupID int unsigned not NULL,
PRIMARY KEY (ObjectID, GroupID)
);
Create Table Objects
(
ObjectID int not null auto_increment primary key,
ObjectName varchar(100) not null,
ObjectOwner varchar(22) not NULL
);
The issue is to find all the Objects that belong to my Groups.
Getting objects in a group is simple enough:
Code:
SELECT ObjectID, ObjectName FROM Objects
WHERE ObjectID IN
(SELECT ObjectID FROM ObjectGroups WHERE GroupID=1)
Finding out my Groups requires a UNION:
Code:
(SELECT GroupID FROM Groups WHERE GroupOwner='sally')
UNION
(SELECT Groups.GroupID AS GroupID FROM GroupMembers
JOIN Groups ON GroupMembers.GroupID=Groups.GroupID
WHERE GroupMembers.Member='sally')
That's what I am using as the subquery to IN for getting a list of GroupIDs that are in my Groups.
Code:
SELECT ObjectID FROM ObjectGroups WHERE GroupID IN
((SELECT GroupID FROM Groups WHERE GroupOwner='sally')
UNION
(SELECT Groups.GroupID AS GroupID FROM GroupMembers
JOIN Groups ON GroupMembers.GroupID=Groups.GroupID
WHERE GroupMembers.Member='sally'))
This is where I stumble into the syntax error.
I suspect perhaps what I am doing is unsupported syntax. But it is saying syntax error not syntax unsupported.
??