How to assign a single nickname to 2 columns of different tables?

2

Is there any way to assign a nickname in 2 columns of different tables in SELECT ?

SELECT
(a.coluna1, b.coluna2) as coluna_geral
FROM
tabela1 a
LEFT JOIN tabela2 b ...
    
asked by anonymous 10.07.2015 / 14:59

1 answer

1

What you are trying to do has no logic whatsoever in programming. If you have two different data in two columns, giving the same name to them will cause you to lose the reference of one of the two. For example:

SELECT
  a.coluna, b.coluna
FROM
tabela1 a
LEFT JOIN tabela2 b ...

 _________________
| coluna | coluna |
|--------|--------|
| NT     | 554    |
|________|________|

The result can be NT or 554 and has a 50% chance of not being what you want.

What is common to happen, is columns with the same name having the same data, for example:

SELECT
  *
FROM
  compras a
     LEFT JOIN usuarios b ON b.userid = a.userid
[...]

 _________________
| userid | userid |
|--------|--------|
| 12     | 12     |
|________|________|

In this case it is good to omit one of the columns:

SELECT
  a.*, b.nome, b.cpf, b.etc
FROM
  compras a
     LEFT JOIN usuarios b ON b.userid = a.userid
[...]

So you already have the user id in the compras table and the rest of the data in the usuarios table.

If you want to print the data from two columns as one, the path is concatenated:

SELECT 
  CONCAT(a.coluna, '-', b.coluna) as coluna
FROM
tabela1 a
LEFT JOIN tabela2 b ...

 ________
| coluna |
|--------|
| NT-554 |
|________|
    
10.07.2015 / 15:58