SQL查询获取结果连接两个表,匹配2个不同的值

时间:2022-12-29 01:55:22

I have 2 table 1st Table is

我有2桌第1桌是

Customer Number, Customer Name, Place, State

  • 1, ABC, DEL, DL
  • 1,ABC,DEL,DL

  • 2, ABD, PNP, HR
  • 2,ABD,PNP,HR

  • 3, ABE, LDH, PB
  • 3,ABE,LDH,PB

Another Table

Code, Description

  • DEL, Delhi
  • DL , Delhi
  • DL,德里

  • PNP , Panipat
  • PNP,Panipat

  • HR , Haryana
  • 人力资源,哈里亚纳邦

  • LDH , Ludhiana
  • LDH,卢迪亚纳

  • PB , Pubjab
  • PB,Pubjab

I need a SQL Query which shows the following Result

我需要一个SQL查询,它显示以下结果

Customer Number, Customer Name, Place, State

  1. 1, ABC, DELHI, DELHI
  2. 1,ABC,DELHI,DELHI

  3. 2, ABD, PANIPAT, HARYANA
  4. 2,ABD,PANIPAT,HARYANA

  5. 3, ABE, LUDHIANA, PUNJAB
  6. 3,ABE,LUDHIANA,PUNJAB

1 个解决方案

#1


2  

Basically, you need to join Table2 twice on Table1 because are two dependent columns on it.

基本上,您需要在Table1上连接Table2两次,因为它上面有两个依赖列。

SELECT  a.CustomerNumber,
        a.CustomerName,
        b.Description Place,
        c.Description State
FROM    Table1 a
        INNER JOIN Table2 b
            ON a.Place = b.Code
        INNER JOIN Table2 c
            ON a.State = c.Code

To further gain more knowledge about joins, kindly visit the link below:

要进一步了解联接,请访问以下链接:

OUTPUT

╔════════════════╦══════════════╦══════════╦═════════╗
║ CUSTOMERNUMBER ║ CUSTOMERNAME ║  PLACE   ║  STATE  ║
╠════════════════╬══════════════╬══════════╬═════════╣
║              1 ║ ABC          ║ Delhi    ║ Delhi   ║
║              2 ║ ABD          ║ Panipat  ║ Haryana ║
║              3 ║ ABE          ║ Ludhiana ║ Pubjab  ║
╚════════════════╩══════════════╩══════════╩═════════╝

#1


2  

Basically, you need to join Table2 twice on Table1 because are two dependent columns on it.

基本上,您需要在Table1上连接Table2两次,因为它上面有两个依赖列。

SELECT  a.CustomerNumber,
        a.CustomerName,
        b.Description Place,
        c.Description State
FROM    Table1 a
        INNER JOIN Table2 b
            ON a.Place = b.Code
        INNER JOIN Table2 c
            ON a.State = c.Code

To further gain more knowledge about joins, kindly visit the link below:

要进一步了解联接,请访问以下链接:

OUTPUT

╔════════════════╦══════════════╦══════════╦═════════╗
║ CUSTOMERNUMBER ║ CUSTOMERNAME ║  PLACE   ║  STATE  ║
╠════════════════╬══════════════╬══════════╬═════════╣
║              1 ║ ABC          ║ Delhi    ║ Delhi   ║
║              2 ║ ABD          ║ Panipat  ║ Haryana ║
║              3 ║ ABE          ║ Ludhiana ║ Pubjab  ║
╚════════════════╩══════════════╩══════════╩═════════╝