| |

VerySource

 Forgot password?
 Register
Search
View: 2570|Reply: 4

Questions about random queries

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-25 15:00:01
| Show all posts |Read mode
Such as the title: I want to randomly select a record from a table A, I used two methods, but the results obtained are completely different, please ask prawns:


method one:
select * from tableA where rownum <2 order by dbms_random.random;

Method Two:
select * from (select * from tableA order by dbms_random.random) where rownum <2;


Method one filters out the same record every time
Method two filter out different records each time

Why is the first method not working? According to reason, both methods are random sorting first, and then filtering. Why doesn't the first method work?
Reply

Use magic Report

0

Threads

114

Posts

69.00

Credits

Newbie

Rank: 1

Credits
69.00

 China

Post time: 2020-9-26 13:15:01
| Show all posts
You can use this sentence:
select top 1 * from tableA order by newid()
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-27 15:00:01
| Show all posts
The first method is to select a record from the records, and then randomly sort this record
Method two is to randomly select all records, and then select the first one

select ... where ... order by First select the records that meet the conditions, and then sort the records. Isn't that the case?
Reply

Use magic Report

0

Threads

22

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-9-27 15:15:01
| Show all posts
tran123

correct
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-9-27 15:30:01
| Show all posts
The first floor is the wording of SQLSERVER
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list