在简单的SELECT / WHERE子句中使用函数

时间:2013-08-08 17:39:00

标签: sql function select syntax where

我可以在SQL(MySQL,SQL Server等)中使用这个简单的(不涉及'set')语法:

SELECT 1+1;
SELECT RAND();

但我不允许我使用它:

SELECT RAND() WHERE ( RAND() < 0.5 )

它没有与sql集的交互,但我认为语法是合法的,应该允许

欢迎提出意见......;)

1 个答案:

答案 0 :(得分:0)

您的查询

select rand() - 从0到1获取随机数 然后通过

过滤它

where rand() < 0.5 - 从0到1得到另一个随机数,如果它&lt; 0.5然后返回带有prevoius数字的1行,否则返回0行

如果你想得到0到0.5之间的随机数,你应该SELECT RAND() * 0.5

<强>更新

;with cte as (
    select * from (select rand() as number) as a where number < 0.5
    union all
    select * from (select rand() as number) as a where number < 0.5
)
select * from cte