2
Good afternoon Galera.
I have a little problem to join some tables by INNER JOIN, and because the columns name are equal, I imagine that the ALIAS can save me by changing the columns name pro front.
Turns out, I’ll have to do this on several pages and many different columns, I can use * SELECT and ask to use Alias in all columns?
Something like SELECT * AS tabelarenomeada_* FROM tabela
to not have to rename column by column? For one of the querys I have as an example is like this:
SELECT turmas.id AS turma_id, turmas.id_professor AS
turma_idprofessor, turmas.id_curso AS turma_idcurso,
turmas.data_inicio, turmas.qtd_alunos,
professores.id AS professor_id, professores.nome_professor AS
professor_nome,
cursos.id AS curso_id
FROM turmas
INNER JOIN professores ON turmas.id_professor = professores.id
INNER JOIN cursos ON turmas.id_curso = cursos.id
WHERE turmas.ativo = 1 AND professores.ativo = 1 AND cursos.ativo = 1
Is there any way to force all columns to use ALIAS with a fixed name in front of the original name for example?
Oh yes, I understood the alias on the chart, but I think I misexpressed myself. What I would like is to be able to use * in the search but somehow rename the column. In this case I will use a lot
SELECT * FROM tabela1 INNER JOIN tabela2
for example. I wish I could continue using * without having to rename column by column. Is that possible? (Ah and sorry for the misconception, when copying the code came badly formatted)– Rafael
you could define so
SELECT * FROM turmas tur INNER JOIN professores PROF ON TUR.id_professor = PROF.id INNER JOIN cursos CUR ON TUR.id_curso = CUR.id
– Leonardo
It is possible yes Rafael vc use the * in an Inner Join... When you set
select coluna1, coluna2 from exemplo
vc restricts the search only to the informed columns... when you use *, all columns of the junction between Table 1 and table2 will be returned.– Leonardo
Got it, so would be perfect my query, but in this case, when I bring result to Frontend the column name will still be the original no? Because my problem is when I will display this in an HTML table when I use id for example, it can be ambiguous, since I didn’t rename it to TUR.id as an example.
– Rafael
In this case yes, the name would be equal to the column name. Only the way to pass * would not be the most correct one. For example: here in the company we use Protheus, your BD has table that has more than 100 columns and the table names are similar to this pattern
SB1010
,SB5010
... Just imagine making a product query bringing all the tables ofSB1010
together with your product complementSB5010
... It would allocate a lot of resource on the machine besides bringing unnecessary data to each query...– Leonardo
With each query, we define the columns and their alias... It takes a little more work but makes it easier to maintain the code and it is also possible to use masks in the column alias in this way:
select B1_COD as [Código], B1_DESC as [Descrição] from SB1010
– Leonardo
This is true. It could turn more headache than solution. Thank you very much for the help and the tips Leonardo. Helped me a lot and clarified a lot too. A hug =]
– Rafael