Hey guys, I'm stuck with a query and I don't know where to go with this. I'll start off by talking about the situation that I'm in (so you guys have some context), then I'll go ahead and ask the question.
Basically I'm creating a game that's constantly interacting with the database. In the database, I have tablesUsers,Games. andUsersGames. This question focuses around theUsersGamestable, so it's structure is most important:
Code:mysql> SHOW COLUMNS FROM UsersGames; +-------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | user | int(11) | NO | | NULL | | | game | int(11) | NO | | NULL | | | role | int(11) | NO | | NULL | | +-------+---------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)userandgameare both foreign keys toUsersandGamesrespectively.
Now, below I'll post my query, explain it, then explain what I want done (but can't figure out).
So basically what we're doing here is selecting rows from theCode:SELECT * FROM Users WHERE id IN (SELECT User FROM UsersGames WHERE (role = :role OR :role = '%') AND (game = :game OR :game = '%'))Userstable where row's ID matches theusercolumn inUsersGameson conditions. The conditions I currently have look to see if the user'sroleis something specifically or the game their in is something specifically. Notice I also have a:parameter = "%"which allows me to just pass "%" for the parameter if I don't want to filter by the column.
Now, what I'm trying to insert into the query above is another column in the output. Essentially if:gameis set to anything else except "%", I want the query to, in addition to selectingUsers.* FROM Users, selectUsersGames.role(whereUsersGames.user = :user AND UsersGames.game = :game). I know I probably have to do something with a union of some sort, I'm just not sure what.
If you guys have any ideas or solutions, please share. I'm reluctant to just letting PHP pull off a couple of simple if statements because that's generally considered bad practice and it'll be most likely quicker and easier to modify for the future in the query regardless. If something is confusing in this question, please don't feel hesitant in asking questions or pointing something out that wasn't quite clear.
Thanks,
Jeremy



Reply With Quote


Bookmarks