Python Sqlite:如何随机选择特定于值的行?

Bar*_*len 2 python sqlite

我希望能够随机选择具有特定值的行。例如,我在 sqlite 中有一个学生表,它存储不同的特征(即性别)。我想使用 python 随机选择一名男性学生。我已经查看了其他问题(例如Select a random row from the table using Python),但与值特定行无关。我该怎么做?

Ado*_*obe 5

不需要 python 特定语法:sqlite 有一个random()函数:

select
  *
from users

where gender == 'M'

order by random()

limit 1
Run Code Online (Sandbox Code Playgroud)

有关性能,请参阅:/sf/answers/1721418191/