How to select multiple columns using the table prefix only once?

Asked

Viewed 433 times

5

In relational model condition, I have to specify the prefix of the table, I’m sure?

Example: SELECT c.nome, c.idade, a.nome, a.idade FROM...

What I wanted to know is if you can’t do something like this:

SELECT c(nome, idade), a(nome, idade) FROM...

Can you give this "streamlined" code, or the only way is to prefix everything?

  • 2

    Unlike this, I think only the SELECT c.*, a.* FROM...

  • That’s the answer, @Islam

  • It is not possible to do this. The bank will try to find a c function and a function in this example: SELECT c(nome, idade), a(nome, idade) FROM

  • @Reginaldorigo But that was exactly the problem. I wasn’t asking literally, but something similar, you know? But I don’t think there really is =(

  • @I was trying to get away from this option because I think it consumes more resources, no?

  • As Ismael said c( nome, idade ) must be c.*

  • Using * indiscriminately will actually consume more processing and network bandwidth, as ALL table fields (of your query) will be required. In addition, it will directly influence the index chosen by SGDB to perform the query. It can have bad consequences when working with Views as well. Particularly, I always declare all fields.

  • Also because table changes can 'break' those codes with *

  • Thanks to all for the clarifications. I will declare everything with prefix even. Thanks! =)

  • There is no way to do this. Each field of the table returned in SELECT is unique, so you have to prefix with the alias or table name if there is more than one field with the same name in the tables used in FROM. Using * is not a good option, as @Smael mentioned earlier. But this "streamlined" you mentioned may not be such an effort, since you mount the query once and do not change all the time, it is worth making a very readable query, easy to understand and will not generate problems later.

  • @inovapixel Hello, consider accepting my answer if it has been useful to you. If you think she’s incomplete or doesn’t respond to you, make the appropriate comments so I can improve her.

Show 6 more comments

1 answer

2

The best solution to your problem will really be the field-to-field declaration with its due Alias.

Obviously you can use the wildcard character *, consider the syntax:

[PREFIX]. * or
[TABLE NAME].*

whereas ALL fields will be invoked, your case would be:

SELECT c.*, a.* FROM...


Using aliases from table

"The readability of an instruction SELECT can be enhanced by assigning an alias to a table, which is also known as correlation name or range variable. A table alias can be assigned with or without the AS keyword:

table_name AS table alias
table_name table_alias

Note. If an alias is assigned to a table, all explicit references to the table in the Transact-SQL will need to use alias; not table name."

Some posts correlated

Prefixes in SQL Sentence
How I make a SELECT in 2 or more Tables with 2 or more conditions?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.