SQLite3 getting output from one table as input to another
Table1 has the following columns
problem t1 t2
t1 and t2 are INT
problem is the resul开发者_如何转开发t of the values from t1,t2
t1 can be 1,2,3,4,5 t2 can be 6,7,8,9,10
I could write an SQL query that says
SELECT * from Table1 WHERE (t1=1 OR t1=2 OR t1=3 OR t1=4 OR t1=5) AND (t2=6 OR t2=7 OR t2=8 OR t2=9 OR t2=10)
But instead of writing out the entire expression (shown above), I want to store it into another table (called Table2)
Table2 has the following columns data_entered data_results
data_entered is 1 data_results is a string with the following data "(t1=1 OR t1=2 OR t1=3 OR t1=4 OR t1=5)"
data_entered is 2 data_results is a string with the following data "(t2=6 OR t2=7 OR t2=8 OR t2=9 OR t2=10)"
so I want to create a QUERY where I could say
SELECT * from Table1 WHERE ... (and specify data_entered) the results should be the same as the above mentioned SQL.
Please help.
Thanks
For your first query, you can use IN instead:
select * from table1 where t1 in (1,2,3,4,5) and t2 in (6,7,8,9,10);
Then instead of storing sql code in table2, store each value in a seperate row.
create table data (entered, result);
insert into data values (1, 1);
insert into data values (1, 2);
...
insert into data values (2, 10);
Then you can do:
select *
from table1
where t1 in (select result from data where entered = 1)
and t2 in (select result from data where entered = 2);
精彩评论