Hi, I am getting random row from my table using the following query:
SELECT value_id FROM table1 ORDER BY RANDOM() LIMIT 1
I have 3 tables: table1 and table2 - they are tables that have an id of a row and its value. there is as well table3 which has as its columns IDs of rows in table1 and table2.
i.e. :
table1:
1 - Canada
2 - USA
3 - UK
4 - France
5 - Africa
6 - China
table2:
1 - New York
2 - Vancouver
3 - Paris
4 - London
5 - Ottawa
6 - Los Angeles
table3: (without table3 rowid)
1 - 2
1 - 5
2 - 1
2 - 6
3 - 4
4 - 3
So from table 3 I can say that New York is a city in USA, and so on. And as well in table3 I can have undefined values. (In the example they are Africa and China so I don't want to get them in a result)
What I want to do is to get from table1 random IDs but only those that are defined in table3. In this particular case I don't want to receive 5 and 6 as a result.
How can I rewrite my query to achieve my goal?
Thank you in advance.
Edit: The problem is that when I am trying to get a row which row in table3 is not defined, application cruses. It is not NULL, as I wrote before, it just doesn't exist in table3. How can I rewrite the query to properly handle my case?