Performing Inner Join for Multiple Columns in the Same Table

后端 未结 1 969
天涯浪人
天涯浪人 2021-01-30 05:34

I have a scenario which I\'m a bit stuck on. Let\'s say I have a survey about colors, and I have one table for the color data, and another for people\'s answers.

tbColor

相关标签:
1条回答
  • 2021-01-30 06:02

    This seems like the way to go:

    SELECT
      A.answer_id
      ,C1.color_name AS favorite_color_name
      ,C2.color_name AS least_favorite_color_name
      ,C3.color_name AS color_im_allergic_to_name
    FROM tbAnswers AS A
    INNER JOIN tbColors AS C1
      ON A.favorite_color = C1.color_code
    INNER JOIN tbColors AS C2
      ON A.least_favorite_color = C2.color_code
    INNER JOIN tbColors AS C3
      ON A.color_im_allergic_to = C3.color_code
    

    Rather than "stupid", I'd venture that this is a pretty standard query. This also presumes that all columns will have a valid value. Otherwise, replace all INNER JOINs with LEFT JOINs

    0 讨论(0)
提交回复
热议问题