开发者

query to join a table with two fields into one list containing items in both fields

Looking at similar questions, I actually want the exact opposite of this: SQL query for getting data in two fields from one column

I have a table meetings with paired users:

A_user_id | B_user_id
1               2
3               4

There is a user table as well.

Is there a simple mysql query that lists all the user_ids into one long list?

query result
1
2
3
4

I was thinking something like this but it doesn't work:

select *
from user 
where user.id in (
    (select A_user_id from meeting)
    or 
    (select B_user_id fro开发者_开发问答m meeting)
)

Thanks!

UPDATE (UNION solved this, but let's make this a bit more challenging):

I want to get a list of usernames and location names (both are reference tables) so I need to join this union query to them. Here's what I tried:

select u1.fname, l1.name
 from meeting m1
 join user u1 on m1.A_user_id=u1.id 
 join locations l1 on m1.location_id=l1.id
union
select u2.fname, l2.name
 from meeting m2
 join user u2 on m2.A_user_id=u2.id 
 join locations l2 on m2.location_id=l2.id
order by location_id asc

I'm getting two errors:

1- Not sure what kind of joins I need on these. (without the last 'order by' line) I'm getting a list of only 2 (there should be 4, as there are 2 pairs of people meeting). It seems to be pulling only the first item from each part of the union. I believe this relates to the type of join I'm doing for each, but not sure. So, users are distinct (there is only 1 user in the meeting table and it matches only 1 user in the user table), but locations are not (2 users are meeting at 1 location, and I think when I join on locations it is messing things up).

2- How do I use the "order by" at the end to order by the resulting list of "location_id"s, since now I have two named tables to deal with.

Thanks!

UPDATE 2:

Ok I put the two selects into parenthesis and UNIONed them and now I can order by the location_id... but I still have no idea how to join on the location table. Mysql doesn't like what I tried

(select u1.fname, m1.location_id
from meeting m1
join user u1 on m1.A_user_id=u1.id)
union
(select u2.fname, m2.location_id
from meeting m2
join user u2 on m2.B_user_id=u2.id)
#join locations l on l.id = location_id // this line messes things up *
order by location_id asc
  • Doesn't there need to be an all encompassing select around this whole thing? How do I join the locations.id field on the "location_id" field that gets kicked off of the union query? Since the "location_id" field is technically in two different tables? THe join above throws an error.

UPDATE 3: SOLVED

Here's my final query:

select tb1.fname, l.name
from (
    (select u1.fname, m1.location_id
    from meeting m1
    join user u1 on m1.A_user_id=u1.id)
    union
    (select u2.fname, m2.location_id
    from meeting m2
    join user u2 on m2.B_user_id=u2.id)
) tb1
join locations l on l.id = tb1.location_id
order by location_id asc


select A_user_id as id from meetings
union
select B_user_id as id from meetings

in your example code, you could use an 'or', but the 'or' has to join two 'in' statements, if you get what I mean.

select *
from user 
where 
(
      (user.id in (select A_user_id from meeting))
   or 
      (user.id in ((select B_user_id from meeting))
)

And to answer you second update, you want something like

select locations.* from
(
   (select A_user_id as id from meeting)
   union
   (select B_user_id as id from meeting)
) as UIDS
join
locations on locations.id = UIDS.id


select A_user_id as user_id from meetings
union all
select B_user_id as user_idfrom meetings
order by user_id

Notes:

  • UNION ALL keeps duplicates, UNION doesn't
  • Any ORDER BY goes at the end of the UNION
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜