sql:对取出的一行数据某些字段进行再次操作


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
主要是一行的数据某些字段整成一个table,对此table进行操作即可
基础:select *, (select orderno) as new from bdorder , 可多次用select操作字段
扩展:可多次用select 取出数据,union成一个table 处理
举例:取出一行中最大的值
  
IF NOT OBJECT_ID('[tb]') IS NULL
    DROP TABLE [tb]
GO
CREATE TABLE [tb](
a SMALLINT,
b SMALLINT,
c SMALLINT,
d SMALLINT,
e SMALLINT,
f SMALLINT,
g SMALLINT 
)
INSERT [tb]
SELECT 1,2,3,4,5,2,3 UNION ALL
SELECT 4,5,6,7,7,2,0 UNION ALL
SELECT 4,9,6,7,7,9,6
GO
--SELECT * FROM [tb]
 
-->SQL查询如下:
 
SELECT *,
    (SELECT MAX(a)
     FROM(
       SELECT a UNION ALL       --用select 取出值union成table
       SELECT b UNION ALL
       SELECT c UNION ALL
       SELECT d UNION ALL
       SELECT e UNION ALL
       SELECT f UNION ALL
       SELECT g
       ) AS t   
    ) AS maxvalue
FROM tb