4
I have a question as to the advantages of having standardised tables and their forms of use within a SELECT. 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 to be able to 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 don’t know another way to display this data with only 1 query in the database.
I know that the use of normalized tables comes from good practices and as security to hinder "SQL Injection" in the system. However the code is not only extensive but still has to be observed so that there are no errors regarding the parameters of the ID informed so that there is no mismatch of the information.
My question is: There is a more practical way to get the same data, without so many (SELECT) embedded in the code?
Hello @Rafaelsalomão - Really, the form you presented is much simpler, and I hadn’t thought about it: instantiating twice the same table with different variables, in fact I didn’t think!... Although I still think that the more complex the bank will always be more laborious and with extensive lines of code, I have to agree that: >follow another path however easy it may seem, at the front will generate serious problems about expansion and maintenance.
– Israel Felipe R. P.