INNER JOIN with 3 sql tables

Asked

Viewed 30 times

0

I have 3 tables in the database:

tbl_cupom_generated:

id_cupom  |    nome
 ------        -----
   40          João 
   60          Maria

tbl_coupon:

  id      |    desconto        |   id_estabelecimento
 ----          --------               -------
  40         50% de desconto             1
  60         20% de desconto             2

tbl_establishment:

  id      |    nome
  ---         ------
   1         Agência X
   2         Agência Y

I would like to integrate the tables in order to relate the id_coupon of tbl_coupon generated with the discount of tbl_coupon and the name of tbl_establishment, in order to give SELECT in the name, discount and name of the establishment. I wrote a code with INNER Joins, but it is not successful. What can I fix?

SELECT nome, tbl_cupom.desconto, tbl_estabelecimento.nome
       FROM tbl_cupom_gerado, tbl_cupom 
       INNER JOIN tbl_cupom ON tbl_cupom.id = tbl_cupom_gerado.id_cupom
       INNER JOIN tbl_estabelecimento ON tbl_estabelecimento.id = tbl_cupom.id_estabelecimento
  • 1

    But are you making a mistake? Or just not returning any data? The only thing "wrong" is that you are not specifying from which table you want the first "name". A tip, create alias for tables for example: From tbl_coupon as c.

  • I wasn’t returning any data, but I was able to correct it. I specified the table data as you commented, and removed tbl_coupon from FROM leaving only tbl_cupom_generated. Thanks for the help

1 answer

0

Try removing the second table in your "FROM". Leave only "FROM tbl_cupom_generated" and try to execute the code.

  • I removed it and it worked! I also specified the tbl_cupom_generated data in SELECT

  • Pasta! I’m glad it worked out. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.