jmp5167
02-25-2010, 11:52 PM
Hello,
I am new to sql and am trying to write an sql statement that ties in several databases into one select call. The idea of the call is to take pieces from each table depending on the user who makes the db call. therefore my function accepts a username argument which gets appended on to the end of the select statement.
[CODE]String sql = "SELECT "+groupsTable+".id,"
+groupsTable+".name AS groupname,"
+"groupowners.username AS groupusername,"
+"groupmembers.username,"
+contactsTable+".id AS contactid,"
+contactsTable+".nickname,"
+contactsTable+".email,"
+contactsTable+".phone"
+" FROM "+groupsTable
//first, join to the users table to get the username of the person who owns the group
+" INNER JOIN "+usersTable+" AS groupowners ON groupowners.id="+groupsTable+".userid"
//now, join to the contacts and users table (via the groupMembers table) to get the members of the group
+" INNER JOIN "+groupMembersTable+" ON "+groupsTable+".id="+groupMembersTable+".groupid"
+" INNER JOIN "+contactsTable+" ON "+groupMembersTable+".contactid="+contactsTable+".id"
+" INNER JOIN "+usersTable+" AS groupmembers ON groupmembers.id="+contactsTable+".contactuserid"
+" WHERE groupowners.username=?;";
[CODE]
for some reason this is not getting me the information I want
any help is much appreciated
I am new to sql and am trying to write an sql statement that ties in several databases into one select call. The idea of the call is to take pieces from each table depending on the user who makes the db call. therefore my function accepts a username argument which gets appended on to the end of the select statement.
[CODE]String sql = "SELECT "+groupsTable+".id,"
+groupsTable+".name AS groupname,"
+"groupowners.username AS groupusername,"
+"groupmembers.username,"
+contactsTable+".id AS contactid,"
+contactsTable+".nickname,"
+contactsTable+".email,"
+contactsTable+".phone"
+" FROM "+groupsTable
//first, join to the users table to get the username of the person who owns the group
+" INNER JOIN "+usersTable+" AS groupowners ON groupowners.id="+groupsTable+".userid"
//now, join to the contacts and users table (via the groupMembers table) to get the members of the group
+" INNER JOIN "+groupMembersTable+" ON "+groupsTable+".id="+groupMembersTable+".groupid"
+" INNER JOIN "+contactsTable+" ON "+groupMembersTable+".contactid="+contactsTable+".id"
+" INNER JOIN "+usersTable+" AS groupmembers ON groupmembers.id="+contactsTable+".contactuserid"
+" WHERE groupowners.username=?;";
[CODE]
for some reason this is not getting me the information I want
any help is much appreciated