简体   繁体   English

sql查询根据之前的查询从表中随机选择一行

[英]sql query to select a random row from a table based on the previous query

I am writing a simple letter guess game where the categories and phrases are stored in a sqlite database.我正在编写一个简单的字母猜谜游戏,其中类别和短语存储在 sqlite 数据库中。 One table (categories) holds the table name and the human readable title of the table.一张表(类别)包含表名和表的可读标题。 There are 31 other tables with the phrases in them.还有其他 31 个表,其中包含短语。

while i can do this with two sql queries, first to retrieve a random table name, and another to retrieve the random phrase from the second table.虽然我可以用两个 sql 查询来做到这一点,第一个检索随机表名,另一个检索第二个表中的随机短语。

"SELECT tablename, title FROM categories order by Random() limit 1";

then assign tablename to the variable table.然后将表名分配给变量表。

"SELECT phrase FROM " + table + " order by Random() limit 1";

What I am trying to figure out is how to combine these statements so that i can store it as a view and then my program doesn't need to know about the internal structure of the database.我想弄清楚的是如何组合这些语句,以便我可以将其存储为视图,然后我的程序不需要知道数据库的内部结构。

I hope i have explained this clearly enough, thank you in advance.我希望我已经足够清楚地解释了这一点,在此先感谢您。

不幸的是,我发现这在不支持像 sqlite 这样的存储过程的数据库上是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM