Performing Inner Join for Multiple Columns in the Same Table

僤鯓⒐⒋嵵緔 提交于 2019-12-02 15:30:44
BradBrening

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!