MYSQL Inner Join if statement

前端 未结 2 628
逝去的感伤
逝去的感伤 2021-01-13 05:11

i\'m trying to join 3 tables together but use an IF statment to only run a certain query

I have a user table, customer and staff table. The first part below works:

2条回答
  •  礼貌的吻别
    2021-01-13 05:31

    Select user.userID
        , username
        , user.email
        , registeredBy, registeredDate
        , Case 
            When User.Level = 2 Then Concat(Level2Staff.firstName, ' ' , Level2Staff.lastName) 
            When User.Level = 1 Then Concat(customer.firstName, ' ' , customer.lastName) 
            End
            AS staffName 
    FROM user
        Inner Join level
            On user.levelID = level.levelID 
        Inner Join staff
            On user.registeredBy = staff.UserID
        Left Join customer
            On customer.userID = user.UserID
                And user.level = 1
        Left Join staff As Level2Staff
            On user.userID = Level2Staff.UserID
    

提交回复
热议问题