3
I have two tables in the database: zip code and users. Can I do a single query to get fields from both tables? Both have idCep. I want to get the address data of the cep table and the name and user of the table users.
As I am working with php, I am using PDO with the database.
For losing normality?
– GustavoSevero
I forgot to mention, but I am using PDO. $dadsUsuario=$Pdo->prepare("SELECT * FROM usuarios JOIN cep ON WHERE cep.idCep=:usuarios.idCep");
– GustavoSevero
Yes, @Gustavosevero, you now have two keys, making it possible to have repeated Zip Codes in the table. See here about Data Normalization.
– Thiago Lunardi
@Gustavosevero: Fix this one
ON WHERE
, follow:$dadosUsuario=$pdo->prepare("SELECT * FROM usuarios JOIN cep ON cep.idCep=:usuarios.idCep");
– Thiago Lunardi
OK, it worked. Just the following, more than one result appeared for this query. I can’t say that both have to be equal to such id? Because ALL the idCep of the cep table have the same idCep for the users table, so will appear numerous results.
– GustavoSevero
@Gustavosevero ERRATA:
$dadosUsuario=$pdo->prepare("SELECT * FROM usuarios JOIN cep ON cep.idCep = usuarios.idCep AND cep.idCep=:usuarios.idCep");
– Thiago Lunardi
Nothing changed in the results kkk Several appear. I want to do something like: SELECT * FROM usuarios JOIN cep ON cep.idCep = usuarios.idCep WHERE idCep=:idCEP
– GustavoSevero