sql 通过列号/索引/位置而不是列名选择列 [英] sql select column via column number/index/position instead of column name
本文介绍了sql 通过列号/索引/位置而不是列名选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过列号/索引/位置而不是列名查询选择列?
How to query select column via column number/index/position instead of column name?
到目前为止这不起作用:
So far this does not work:
SELECT 1,2,3,4,5,6,7 from table_1
Union
SELECT 1,2,3,4,5,6,7 from table_2
推荐答案
如果 DB 支持公用表表达式(除 MySQL 外的所有),您可以为列指定新名称:
If the DB supports common table expressions (all except MySQL), you can give the columns new names:
WITH cte(c1, c2, c3, c4, c5, c6, c7) AS (
SELECT * FROM table_1
UNION
SELECT * FROM table_2
)
SELECT c1, c2, c3, c4, c5, c6, c7 FROM cte;
但是,这仅在您可以使用 SELECT *
时才有效,因此两个表必须具有相同的已知列数.
However, this works only if you can use SELECT *
, so both tables must have the same, known number of columns.
在 MySQL 中,您可以使用带有列名的视图:
In MySQL, you could use a view with column names instead:
CREATE VIEW v(c1, c2, c3, c4, c5, c6, c7) AS SELECT ...;
这篇关于sql 通过列号/索引/位置而不是列名选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文