How to do the select of two tables in mysql? [duplicate]

0

I know this should be an easy question, but I'm a beginner:

I have these 3 tables in my bank:

In this case, the purchased table is where the cpf of the user who purchased the course is stored and saves the course id. I think that's right,

So, what I want to do is a select that returns the course that the user is enrolled in.

How to do this?

    
asked by anonymous 06.02.2018 / 23:09

1 answer

1

Dude, your tables are not optimized and you're going to have problems later with names, references, etc., such as using the CPF as a user id in the purchasing table. The most appropriate would be to use an auto-increment for a sequential ID and then use it.

Think that when using the CPF you may have problems with the searches (easy to miss typing, will have to deal with cases of invalid cpf's, etc.).

I also think it was not cool to allow the table to be bought with the 2 fields set to 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 about crossing data between tables 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;
    
06.02.2018 / 23:42