0

I would like to display a <table> with these <th>-s:
project title
description
status
start date
end date
users

The problem is, that my subquery returns more than 1 row. Usually 4 people belong to a project, an owner, a manager, a developer and a tester. The owner can create new projects, so he can see the project table I would like to create, but I can't put the users at the end of the table, because more than one belongs to every row.

This is the query:

SELECT project.id, title, description, status, start,end
        FROM project 
        LEFT JOIN users ON (SELECT userid FROM roles,project WHERE projectid=project.id)=users.id

My tables are:

PROJECT
id
title
description
status
start
end

USERS
id
name
pass
email

ROLES
id
projectid
userid
rolename

darksoul90
  • 145
  • 2
  • 16
  • 1
    See GROUP_CONCAT for how to quickly get a denormalized "users" text column - that is "multiple rows to one column". Use a GROUP BY as well. This works OK for denormalizing single values such as names (but does poor at handling associated values). Alternatively, do the processing in the client. – user2864740 Apr 26 '14 at 19:20
  • 1
    can you provide some data and expected output ? – Abhik Chakraborty Apr 26 '14 at 19:23

3 Answers3

1

Use an IN clause, e.g.

JOIN users ON users.id IN (SELECT userid FROM ....)

= is for comparing two single values. IN is used to check if a single value is in a set of values.

Marc B
  • 356,200
  • 43
  • 426
  • 500
1

Try join all tables instead of subquery like this :

    SELECT project.id, title, description, status, start,`end` ,GROUP_CONCAT( users.name) AS usernames
    FROM project 
    LEFT JOIN roles On roles.projectid = project.id 
    LEFT JOIN users ON roles.userid =users.id
    GROUP BY project.id
echo_Me
  • 37,078
  • 5
  • 58
  • 78
1

I assume you want all the users in a cell of your result table but only one copy of the other fields in their cells. If you really want this in one table you need to parse the results in code. Use a query that has all the fields you want like so:

SELECT project.id, title, description, status, start,end, users.name
FROM project
LEFT JOIN roles On roles.projectId = project.id
LEFT JOIN users ON roles.userId = users.id

Then in your code check for the duplicates and don't display them. Pseudo code example:

loop over all result rows {
   if( new_id <> old_id ){
      if not first row close your previous row </tr>
      display <tr>
      display all the fields
   }else{
       only display the addition of the user name (with a <br> or whatever to separate)
   }
}
close your last row </tr>

This will probably display it the way you want but the data is not aligned for one table of display. Consider a link to show the user list as a popup DIV.

Arrowsmith
  • 122
  • 1
  • 9