简体   繁体   中英

How can I conditionally join two MySQL tables on multiple fields?

In my MySQL DB, I have 2 tables: pos_tables and pos_user_login . In pos_tables , I have four staff fields: staff1 , staff2 , staff3 , staff4 . These 4 fields hold the IDs of staffmembers stored in pos_user_login . In pos_tables , these staff IDs may repeat, but staff_id is the primary key in pos_user_login .

What I'm trying to do is to join these two tables, using a condition of

if pos_user_login.staff_id=(pos_tables.staff1 OR
                            pos_tables.staff2 OR
                            pos_tables.staff3 OR
                            pos_tables.staff4)

... and if any of the 4 fields from pos_tables matches the primary key from pos_user_login , then fetch that record from pos_tables .

What query do I need to run to do this?

---- EDIT ----

Should I use something like this?

SELECT *
    FROM pos_user_login pu
    WHERE pos_user_login_id IN (SELECT *
            FROM pos_tables
            WHERE staff1 = pu.pos_user_login_id
            OR staff2 = pu.pos_user_login_id
                                           ...);

---- EDIT 2 ----

SAMPLE RECORD

pos_tables
----------
table_number (1   )
staff1    (10)
staff2    (11)
staff3    (12)

pos_user_login
--------------
staff_id (10),
type (drinks_person)
staff_id(11), type (order_taker)
staff_id(12), type(cleaner)

After comparing using the WHERE condition (type="drinks_person") , the output should be:

table_number(1), staff1(10), staff2("null"), staff3(null)

You can use the IN() operator:

SELECT *
FROM pos_tables JOIN pos_user_login 
  ON pos_user_login.staff_id IN (
    pos_tables.staff1,
    pos_tables.staff2,
    pos_tables.staff3,
    pos_tables.staff4
  ) 

Sort of sounds like the data hasn't been stored very well. Can you post a schema?

Here's my suggestion: using ifnull and doing multiple joins you can work out if one of the id's has a join. I've returned "name" assuming that the pos_user_login has that field, but you would return any field you need.

SELECT 
     ifnull(p1.id,
        ifnull(p2.id,
           ifnull(p3.id,
              ifnull(p4.id,
                  'no matching record',
              p4.name,
           p3.name,
        p2.name,
     p1.name) as staff_name
FROM
     pos_tables t
      LEFT JOIN pos_user_login p1 on staff1=t.id
      LEFT JOIN pos_user_login p2 on staff2=t.id
      LEFT JOIN pos_user_login p3 on staff3=t.id
      LEFT JOIN pos_user_login p4 on staff4=t.id

There it´s

select  a.*
from    pos_tables a left join
        pos_user_login b1 on a.staff1=b1.staff_id left join
        pos_user_login b2 on a.staff2=b2.staff_id left join
        pos_user_login b3 on a.staff3=b3.staff_id left join
        pos_user_login b4 on a.staff4=b4.staff_id
where  (b1.staff_id is not null or 
        b2.staff_id is not null or
        b3.staff_id is not null or
        b4.staff_id is not null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM