Home  >  Q&A  >  body text

Query data from multiple tables using SQL

Suppose we have three tables:

Table 1

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

Table 2

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

table 3

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

Suppose if some values ​​in dup_id in Table1 are equal to dup_id in Table2, then we can find the dup_id# corresponding to dup_id ##, and use it to find description in Table3. What's the best way to do this?

Output:

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

P粉151720173P粉151720173180 days ago277

reply all(1)I'll reply

  • P粉288069045

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

    It seems to be a direct connection of 3 tables:

    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;

    reply
    0
  • Cancelreply