2

我在 SQL Server 数据库中有几列,如下所示:

Column1                       Column2   
Item1                         <div id="html"><img.............>5424</div>
Item2                         <div id="html"><img.............>3424</div>
Item3                         <div id="html"><img.............>8424</div>
Item4                         <div id="html"><img.............>6424</div>

我想根据内部的值使用 ORDER BY 对它们重新排序>*</div>,以便输出如下所示:

Column1                       Column2   
Item2                         <div id="html"><img.............>3424</div>
Item1                         <div id="html"><img.............>5424</div>
Item4                         <div id="html"><img.............>6424</div>
Item3                         <div id="html"><img.............>8424</div>

我正在使用以下代码从数据库中提取值:

SELECT 
    Table1_1.F1 AS Column1, Table2_1.1PrisHTML AS Column2
FROM     
    Table2 AS Table2_1 
CROSS JOIN
    Table1 AS Table1_1

UNION ALL

SELECT 
    Table1_1.L1, Table2_1.2PrisHTML
FROM     
    Table2 AS Table2_1 
CROSS JOIN
    Table1 AS Table1_1

UNION ALL

SELECT 
    Table1_1.M1, Table2_1.3PrisHTML
FROM     
    Table2 AS Table2_1 
CROSS JOIN
    Table1 AS Table1_1

UNION ALL

SELECT 
    Table1_1.T1, Table2_1.4PrisHTML
FROM
    Table2 AS Table2_1 
CROSS JOIN
    Table1 AS Table1_1

有人有什么建议吗?

编辑:
我确实在单独的列中有值,称为Column3可用于排序的列。但我不想显示此列。有什么建议么?

4

1 回答 1

3

只需Order By在 SQL 查询的末尾添加一个子句。无需将其添加到选择查询中。

我会这样做:

    SELECT Column1, Column2 
    FROM
    (
    SELECT Table1_1.F1 AS Column1, Table2_1.1PrisHTML AS Column2, Column3 
    FROM     Table2 AS Table2_1 CROSS JOIN
                      Table1 AS Table1_1
    UNION ALL
    SELECT Table1_1.L1 AS Column1, Table2_1.2PrisHTML AS Column2, Column3 
    FROM     Table2 AS Table2_1 CROSS JOIN
                      Table1 AS Table1_1
    UNION ALL
    SELECT Table1_1.M1 AS Column1, Table2_1.3PrisHTML AS Column2, Column3 
    FROM     Table2 AS Table2_1 CROSS JOIN
                      Table1 AS Table1_1
    UNION ALL
    SELECT Table1_1.T1 AS Column1, Table2_1.4PrisHTML AS Column2, Column3 
    FROM     Table2 AS Table2_1 CROSS JOIN
                      Table1 AS Table1_1
    )
    ORDER BY Column3
于 2013-07-09T21:46:27.760 回答