How to make a select
by taking a field from each table?
I want to get the text field from 4 different tables, how should I do it?
How to make a select
by taking a field from each table?
I want to get the text field from 4 different tables, how should I do it?
I believe this is what you want:
tables:
TB_USUARIO | TB_ENDERECO | TB_CEP | TB_CIDADE
------------|-----------------------|---------------|-----------
ID_USUARIO | ID_ENDERECO | ID_CEP | ID_CIDADE
NOME | ID_USUARIO | ID_ENDERECO | ID_CEP
ENDERECO | CEP | CIDADE
Method 1:
SELECT
TB_USUARIO.ID_USUARIO,
TB_USUARIO.NOME,
TB_ENDERECO.ID_ENDERECO,
TB_ENDERECO.ID_USUARIO,
TB_ENDERECO.ENDERECO,
TB_CEP.ID_CEP,
TB_CEP.ID_ENDERECO,
TB_CEP.CEP,
TB_CIDADE.ID_CIDADE,
TB_CIDADE.ID_CEP,
TB_CIDADE.CIDADE
FROM TB_USUARIO
INNER JOIN TB_ENDERECO
ON TB_ENDERECO.ID_USUARIO = TB_USUARIO.ID_USUARIO
INNER JOIN TB_CEP
ON TB_CEP.ID_ENDERECO = TB_ENDERECO.ID_ENDERECO
INNER JOIN TB_CIDADE
ON TB_CIDADE.ID_CEP = TB_CEP.ID_CEP
Method 2: Adding nicknames to tables:
SELECT
usu.ID_USUARIO,
usu.NOME,
en.ID_ENDERECO,
en.ID_USUARIO,
en.ENDERECO,
cep.ID_CEP,
cep.ID_ENDERECO,
cep.CEP,
cid.ID_CIDADE,
cid.ID_CEP,
cid.CIDADE
FROM TB_USUARIO usu
INNER JOIN TB_ENDERECO en ON (end.ID_USUARIO = usu.ID_USUARIO)
INNER JOIN TB_CEP cep ON (cep.ID_ENDERECO = en.ID_ENDERECO)
INNER JOIN TB_CIDADE cid ON (cid.ID_CEP = cep.ID_CEP)
Method 3: Returning all table fields:
SELECT * FROM TB_USUARIO usu
INNER JOIN TB_ENDERECO end ON (end.ID_USUARIO = usu.ID_USUARIO)
INNER JOIN TB_CEP cep ON (TB_CEP.ID_ENDERECO = end.ID_ENDERECO)
INNER JOIN TB_CIDADE cid ON (TB_CIDADE.ID_CEP = TB_CEP.ID_CEP)
These are examples of how it can be done, there are several more!
It can be done this way:
SELECT a.name, b.term_taxonomy_id
FROM wp_terms a, wp_term_taxonomy b
WHERE a.term_id=b.term_id