I have a question about the advantages of having standard tables and how to use them within a SELECT. Let's suppose the following tables:
--------------
| tb_endereco |
--------------
| id |
| rua |
| numero |
| bairro |
| latitude |
| longitude |
|______________|
--------------
|tb_solicitacao|
--------------
| id |
| id_origem |
| id_destino |
|______________|
--------------
| tb_origem |
--------------
| id |
| id_endereco |
| responsavel |
| observacoes |
|______________|
--------------
| tb_destino |
--------------
| id |
| id_endereco |
| responsavel |
| observacoes |
|______________|
Suppose, also, that the customer needs a table with the following information regarding Delivery number 1:
----------------------------------------------------------------------------------
| rua_origem | lat_origem | long_origem | rua_destino | lat_destino | long_destino |
----------------------------------------------------------------------------------
In case you can return the data as requested SELECT should be:
SELECT
(SELECT ende.rua FROM tb_endereco AS ende
INNER JOIN tb_origem AS orig ON orig.id_endereco = ende.id
INNER JOIN tb_solicitacao AS entr ON entr.id_origem = orig.id
WHERE entr.id = 1) AS 'rua_origem',
(SELECT ende.latitude FROM tb_endereco AS ende
INNER JOIN tb_origem AS orig ON orig.id_endereco = ende.id
INNER JOIN tb_solicitacao AS entr ON entr.id_origem = orig.id
WHERE entr.id = 1) AS 'lat_origem',
(SELECT ende.longitude FROM tb_endereco AS ende
INNER JOIN tb_origem AS orig ON orig.id_endereco = ende.id
INNER JOIN tb_solicitacao AS entr ON entr.id_origem = orig.id
WHERE entr.id = 1) AS 'long_origem',
(SELECT ende.rua FROM tb_endereco AS ende
INNER JOIN tb_destino AS dest ON dest.id_endereco = ende.id
INNER JOIN tb_solicitacao AS entr ON entr.id_origem = dest.id
WHERE entr.id = 1) AS 'rua_destino',
(SELECT ende.latitude FROM tb_endereco AS ende
INNER JOIN tb_destino AS dest ON dest.id_endereco = ende.id
INNER JOIN tb_solicitacao AS entr ON entr.id_origem = dest.id
WHERE entr.id = 1) AS 'lat_destino',
(SELECT ende.longitude FROM tb_endereco AS ende
INNER JOIN tb_destino AS dest ON dest.id_endereco = ende.id
INNER JOIN tb_solicitacao AS entr ON entr.id_origem = dest.id
WHERE entr.id = 1) AS 'long_destino'
FROM tb_solicitacao WHERE tb_solicitacao.id = 1
I do not know of another way to display this data with only 1 query in the database.
I know that the use of standard tables comes from good practices and as a form of security to hinder "SQL injection" in the system. However the code besides being extensive still has to be observed so that there are no errors regarding the ID parameters informed so that there is no mismatch of the information.
My question is: Is there a more practical way of getting the same data, without so many (SELECT) embedded in the code?