SQLITE random value/row with criteria

Ablia :

So, i know that the question about getting a random row in SQL database have been posted (even more than one time), and it helped me get to this SQL query:

"SELECT column FROM table WHERE id IN (SELECT id FROM table ORDER BY RANDOM() LIMIT 1)"; 

if i understand that well, order by Random is not efficient on large table but if you use it with the id it's fast enough.

So i've tried it, it seems to work well, but then i wanted to add a criteria, so i've changed it just a little bit:

"SELECT column1 FROM table WHERE (column2 LIKE '" + value + "' AND id IN (SELECT id FROM table ORDER BY RANDOM() LIMIT 1)"; 

To be more clear, i have this table: uploaded = (id, filename, owner). I want to select a random filename from a given owner. Which gives me that:

"SELECT filename FROM uploaded WHERE (owner LIKE '" + owner + "' AND id IN (SELECT id FROM uploaded ORDER BY RANDOM() LIMIT 1))";

Problem is: sometimes, rs.next is false (which mean i don't have a result). However, i print the DB before searching for the filename, so i am SURE that the owner does have a related filename in this DB. Maybe only one, but still one.

This bring me to the question: what's wrong with my query?

AxelH :

You want to get a random id for owner, not any ID. That's why you get an empty result sometime. You get an ID that's not for OWNER, so both condition are not TRUE.

So add the condition in this query :

SELECT id FROM uploaded where owner = ? ORDER BY RANDOM() LIMIT 1

No need to check twice for owner, you already got an id for him. So :

SELECT filename FROM uploaded WHERE id IN (
    SELECT id FROM uploaded where owner LIKE ? ORDER BY RANDOM() LIMIT 1
)

PS: this is using a PreparedStatement notation.

PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, owner);
ResultSet rs = stmt.executeQuery();

I suggest you check it to prevent any SQL injection or you can replace ? by your String.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116481&siteId=1