Tuesday, 17 September 2013

Querying a MySQL database to show a selected row and ALL rows

Querying a MySQL database to show a selected row and ALL rows

I have a problem at the moment where I'm trying to get all rows plus a
selected row from my tables.
So I have three tables - users, tasks, and a joining table called tasks_users
The latter table just contains two columns - task_id and user_id, for the
purpose of normalization.
I have a form to edit a task. The problem arises when I want to retrieve
ALL users (so the task can be reassigned) and I also want to show the
CURRENT user. Given my current queries I can only get all the users, or
just the single related user.
Am I overlooking something obvious, or do I need a more complex query? Or
perhaps a couple of queries?
Further clarification:
Imagine you have an "edit task" form and on it you have the user currently
assigned. I want to also show all other users, so that I may reassign.
Given that I have three tables I'm struggling to construct a query that
returns what I want. Currently I can only return ALL user OR the single
assigned user.

No comments:

Post a Comment