Re: How to Query 2 Tables (Not a join?)
One way to do this is to use the union statement which is standard in most modern SQLs. The order by clause comes after all the unions. For two tables tablea and tableb this looks like:
select * from tablea where level=1 union select * from tableb where level=1 order by reverse(rand()) limit 1;
For three tables a,b,c:
select * from tablea where level=1 union select * from tableb union select * from tablec where level=1 order by reverse(rand()) limit 1;
and so on
The tables must all have the same columns for this to work.
This works on MySQL but is probably not efficient - it may hit your server hard if you have as many as 50 tables. You don't say how large these tables are or are expected to become.
You should consider amalgamating the tables into one with an extra column to distinguish between a,b,c and so on as rgcote suggested. You may need to do this if the performance is poor.
__________________
There may be no such thing as a silly question but the world is littered with silly answers.
|