Alucard
Alucard

Reputation: 1902

select a range from multiple tables named by date

The tables have the same structure and are named like (year and month attached):

- tb_201302
- tb_201303
- tb_201304

Each table has a field date_insert that contains the exact insert time in timestamp.

Question: I want to be able to do a selection from those multiple tables, something like (in pseudo code):

SELECT * FROM tb_201302, tb_201303 WHERE date_insert > '02/15' and date_insert < '03/25'

'02/05' and '03/25' would be timestamps normally

So it's basically selectin a range in multiple tables.

I also tried using UNION

SELECT * FROM (
(SELECT * FROM tb_201302 WHERE date_insert > '02/15' and date_insert < '03/25')
UNION
(SELECT * FROM tb_201303 WHERE date_insert > '02/15' and date_insert < '03/25')
) results

But I'm getting a syntax error, unexpected '(', expecting SELECT near '(SELECT * FROM tb_201202

Any ideas ?

Upvotes: 0

Views: 275

Answers (2)

barryhunter
barryhunter

Reputation: 21091

Assumning this really is a SphinxQL question (the error message appears to be a Sphinx produced NOT a MySQL produced error)...

Your first attempt is right, just that you need to convert the actual timestamp, to be a numberic. Can use UNIX_TIMESTAMP() in creating your index, so that the sphinx attribute contains a unix timestamp.

Can then convert it to the same numeric

SphinxQL> SELECT * FROM tb_201302, tb_201303 
          WHERE date_insert > 1360886400 AND date_insert < 1364169600

(strtotim() in PHP can be used to get a timestamp from a date string)

Upvotes: 2

dagfr
dagfr

Reputation: 2384

SELECT * FROM (
    SELECT * FROM tb_201302 WHERE date_insert > '02/15' and date_insert < '03/25'
    UNION
    SELECT * FROM tb_201303 WHERE date_insert > '02/15' and date_insert < '03/25'
) results

Try this

Upvotes: 1

Related Questions