开发者

Add all the columns of a consult in SQL

I have a开发者_如何学Python table with columns

[a1,a2,a3,a4,a5....an]

all the values are numeric. And I'd like a consult that returns:

 [a1,a2,a3,a4,a5....an, S]

Where S= a1+a2+a3+...+an

Is possible to do that without specify the column names in the S calculation? (for don't change that part if some column is added or removed)

Anyway whats the best solution I can use?

Thanks in advance


There is no way to omit naming all the columns. Also, you are working against how relational databases are intended to be used.

Instead create a table which has metacolumns, for example:

CREATE TABLE `tableA` (
 `id`           int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
 `col_index`    int UNSIGNED NOT NULL,
 `value`        int NOT NULL,
  ...
);

To retrieve the equivalent of a5:

SELECT value
 FROM `tableA`
 WHERE col_index = 5  AND (whatever selects the proper rows)


SELECT a1, a2, a3, ... , an, (a1 + a2 + ... + an) AS S FROM yourColumn.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜