我有一个表Test_Person和Test_Person_Details
Test_Person
ID (PrimaryKey)
STATUS "NEW/OLD"
Test_Person_Details
ID (FK)
NAME
AGE
DESC
...
我必须为给定的ID随机获取10个名称WHERE STATUS为“NEW”.. DB2中的查询是什么?
答案 0 :(得分:2)
这是一个缓慢的方式:
select pd.name
from (select p.*
from test_person p
order by rand()
fetch first 10 rows only
) p join
test_person_details pd
on p.id = pd.id;
如果您的表格超过1000行,您可以通过以下方式提高效率:
select pd.name
from (select p.*
from test_person p tablesample bernoulli(1)
order by rand()
fetch first 10 rows only
) p join
test_person_details pd
on p.id = pd.id;