简体   繁体   中英

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. 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.

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.

"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 这样的存储过程的数据库上是不可能的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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