1

I used this first time.

SELECT classroom .cls_id, 
   classroom.cls_name, 
   u1.users_id as users_id1,
   u2.users_id as users_id2
FROM   classroom 
   INNER JOIN clsown 
           ON clsown.cls_id = classroom .cls_id 
   INNER JOIN users AS u1 
           ON clsown.users_id = u1.users_id 
   INNER JOIN users AS u2
           ON clsown.users_id = u2.users_id 

I get this:

cls_id  cls_name  users_id1  users_id2

1 -------- room1 ------- 1 --------- 1
1 -------- room1 ------- 2 --------- 2
2 -------- room2 ------- 3 --------- 3
2 -------- room2 ------- 4 --------- 4

i want to see

cls_id  cls_name  users_id1  users_id2

1 -------- room1 ------- 1 --------- 2
2 -------- room2 ------- 3 --------- 4

I do not know how to do inner join. What am i doing wrong?

Thank for answer.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Harajukuzz
  • 45
  • 6

3 Answers3

1

The query that you are noting here would actually require a pivot as you want to take the unique values for cls_id and cls_name and then order the users into columns.

I wrote this article back in 2009 that shows how to do something like this. It should help

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
0

I assume that you are using Mysql as your RDBMS, if so you can make use of GROUP_CONCAT() to display the records as per your need, but in a different form. i.e] This query will stuff all the additional coloumns you wanted, into a single cell separated by a separator.

SELECT classroom .cls_id, 
       classroom.cls_name, 
       GROUP_CONCAT(u1.users_id ORDER BY u1.users_id SEPARATOR ', ') Users 
FROM   classroom 
       INNER JOIN clsown 
               ON clsown.cls_id = classroom .cls_id 
       INNER JOIN users AS u1 
               ON clsown.users_id = u1.users_id
       Group by classroom .cls_id

The above query will return records like below,

cls_id  cls_name    users

  1      room1       1,2

  2      room2       3,4 

EDIT: Reference for achieving the above result in Sql server

Community
  • 1
  • 1
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

Since you are using SQL Server there are a few ways that you can pivot the data.

You can use an aggregate function with a CASE expression:

select cls_id,
    cls_name,
    MAX(case when rn = 1 then users_id end) user_id1,
    MAX(case when rn = 2 then users_id end) user_id2
from
(
    SELECT cr.cls_id, 
        cr.cls_name, 
        u1.users_id,
        ROW_NUMBER() over(partition by cr.cls_id order by u1.users_id) rn
    FROM classroom cr
    INNER JOIN clsown co
        ON co.cls_id = cr.cls_id 
    INNER JOIN users AS u1 
        ON co.users_id = u1.users_id 
) d
group by cls_id, cls_name;

Or you can use the PIVOT function:

select cls_id,
    cls_name,
    user_id1, 
    user_id2
from
(
    SELECT cr .cls_id, 
        cr.cls_name, 
        u1.users_id,
        'user_id'+cast(ROW_NUMBER() over(partition by cr.cls_id order by u1.users_id) as varchar2)) col
    FROM classroom cr
    INNER JOIN clsown co
        ON co.cls_id = cr.cls_id 
    INNER JOIN users AS u1 
        ON co.users_id = u1.users_id 
) d
pivot
(
    max(users_id)
    for col in (user_id1, user_id2)
) piv;
Taryn
  • 242,637
  • 56
  • 362
  • 405