Heim  >  Fragen und Antworten  >  Hauptteil

Fragen Sie Daten aus mehreren Tabellen mit SQL ab

Angenommen, wir haben drei Tische:

Tabelle 1

|     c_id          |   categories     |   
|-------------------|------------------|
|      7            |       a          |     
|      4            |       b          |     
|      3            |       c          |

Tabelle 2

|     c_id          |   dup_id     |   
|-------------------|--------------|
|       9           |     10       |     
|       5           |      3       |     
|       6           |      2       |  

Tabelle 3

|     c_id          |  description |   
|-------------------|--------------|
|      22           |     xxxx     |     
|       5           |     yyyy     |     
|      11           |     zzzz     |  

Angenommen, dup_id中的某些值等于Table2中的dup_id,那么我们可以找到dup_id对应的dup_id,并利用它来查找Table3中的description in Tabelle 1. Wie geht das am besten?

Ausgabe:

|     c_id          |  description |   
|-------------------|--------------|    
|       5           |     yyyy     |

P粉151720173P粉151720173180 Tage vor270

Antworte allen(1)Ich werde antworten

  • P粉288069045

    P粉2880690452024-04-04 14:26:03

    似乎是 3 个表的直接连接:

    select Table2.c_id, description
    from Table1
    join Table2 on Table1.c_id = Table2.dup_id
    join Table3 on Table2.c_id = Table3.c_id;

    Antwort
    0
  • StornierenAntwort