这是我的查询,我想按百分比和随机顺序获取数据,我明白了 错误:
关键字“ORDER”附近的语法不正确。
这里有什么问题?
string sql = @"SELECT * FROM
(
select * from (
SELECT TOP 100 PERCENT * FROM tbl_adv WHERE Stars = 7 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NEWID()) a
UNION
select * from (
SELECT TOP 84 PERCENT * FROM tbl_adv WHERE Stars = 6 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) b
UNION
select * from (
SELECT TOP 72 PERCENT * FROM tbl_adv WHERE Stars = 5 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) c
UNION
select * from (
SELECT TOP 60 PERCENT * FROM tbl_adv WHERE Stars = 4 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) d
UNION
select * from (
SELECT TOP 48 PERCENT * FROM tbl_adv WHERE Stars = 3 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) e
UNION
select * from (
SELECT TOP 36 PERCENT * FROM tbl_adv WHERE Stars = 2 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) f
UNION
select * from (
SELECT TOP 24 PERCENT * FROM tbl_adv WHERE Stars = 1 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) g
UNION
select * from (
SELECT TOP 12 PERCENT * FROM tbl_adv WHERE Stars = 0 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) h
)
ORDER BY Stars DESC, UpdateDate DESC";
答案 0 :(得分:1)
我认为你只需要给你的表别名。
string sql = @"SELECT * FROM
(
select * from (
SELECT TOP 100 PERCENT * FROM tbl_adv WHERE Stars = 7 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NEWID()) a
UNION
select * from (
SELECT TOP 84 PERCENT * FROM tbl_adv WHERE Stars = 6 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) b
UNION
select * from (
SELECT TOP 72 PERCENT * FROM tbl_adv WHERE Stars = 5 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) c
UNION
select * from (
SELECT TOP 60 PERCENT * FROM tbl_adv WHERE Stars = 4 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) d
UNION
select * from (
SELECT TOP 48 PERCENT * FROM tbl_adv WHERE Stars = 3 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) e
UNION
select * from (
SELECT TOP 36 PERCENT * FROM tbl_adv WHERE Stars = 2 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) f
UNION
select * from (
SELECT TOP 24 PERCENT * FROM tbl_adv WHERE Stars = 1 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) g
UNION
select * from (
SELECT TOP 12 PERCENT * FROM tbl_adv WHERE Stars = 0 and IsSpecial=true and Confirm='1' and show=true and ExpireDate >=@ExpireDate ORDER BY NewId()) h
) Results
ORDER BY Results.Stars DESC, Results.UpdateDate DESC";
答案 1 :(得分:0)
为您订购的SELECT
定义表别名。所以只需在ORDER BY
子句之前添加一个表名。