How to select two tables in mysql?

Asked

Viewed 2,255 times

0

I know this must be an easy question, but I’m a beginner:

I have these 3 tables in my bank:

inserir a descrição da imagem aqui

In this case, the purchased table is where the user’s Cpf is stored that acquired the course and saves the course id. I think this is right,

Hence, what I want to do is a select that returns the course the user is registered.

How to do this?

  • Search on join. I would add that storing CPF as a whole is not a good idea. As much as it looks like a number, it is actually a text made up of numeric characters. To verify this, just think that 011 111 111 11 is different from the value 11 111 111 11. When represented as integer, the left zero is discarded.

  • There are a number of links answering your question in this post: https://answall.com/questions/267808/70

1 answer

1


Dude, your tables aren’t optimized and you’re going to have problems down the road with names, references, etc., like using CPF as a user id in the shopping table. The most appropriate would be to use an auto-increment for a sequential ID and then use it.

Think that when using CPF you may have problems with searches (easy to misspell, you will have to deal with invalid Cpf cases, etc.).

I also think it’s not cool you allow the table purchased with the 2 fields set in null, so what would you store in a case of double nulls?

But since you are a beginner, then this is normal. Do not worry, here is a link for you to learn some basic things relating to cross data between tables and that will make all the difference.

Meanwhile, this is the code you need:

select cursos.nome, usuarios.nome
from cursos, usuarios, comprados
where comprados.usuarios_id = usuarios.cpf
and  comprados.cursos_id = cursos.id;
  • Is there a better way to describe why using an integer in auto increment would be better than using the CPF as a key? The argument that it is easy to misspell the typing and validation have seemed very weak, because any number is easy to misspell the typing and validation is necessary in any situation, key or not.

  • 1

    In case the purchased table is null the two fields, because it can only have a record in it in the case of purchase made, but I’m still riding, and thanks for the link and the reply. Helped a lot..

Browser other questions tagged

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