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

时间:2021-11-09 22:50:47

I can use this simple (no 'set' involved) syntax in SQL (MySQL, SQL Server, etc.):

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

SELECT 1+1;
SELECT RAND();

but I wont allow me to use this:

但我不允许我使用这个:

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

It has no interaction with a sql set but I think that the syntax is legal and this should be allowed

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

Ideas are welcome... ;)

欢迎提出想法...;)

1 个解决方案

#1


0  

your query does

你的查询呢

select rand() - get random number from 0 to 1 and then filter it by

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

where rand() < 0.5 - getting another random number from 0 to 1 and if it < 0.5 then return 1 row with prevoius number otherwise return 0 rows

其中rand()<0.5 - 从0到1得到另一个随机数,如果它<0.5则返回1行prevoius数,否则返回0行

if you want to get random number from 0 to 0.5, you should do SELECT RAND() * 0.5

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

update

;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

#1


0  

your query does

你的查询呢

select rand() - get random number from 0 to 1 and then filter it by

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

where rand() < 0.5 - getting another random number from 0 to 1 and if it < 0.5 then return 1 row with prevoius number otherwise return 0 rows

其中rand()<0.5 - 从0到1得到另一个随机数,如果它<0.5则返回1行prevoius数,否则返回0行

if you want to get random number from 0 to 0.5, you should do SELECT RAND() * 0.5

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

update

;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