Hi,
I have to do several queries that stores data in some temporary tables,
For almost each, I need to filter using a certain preselection,
let me give you an example:
Code:
DROP TEMPORARY TABLE IF EXISTS tmp_data1;
CREATE TEMPORARY TABLE IF NOT EXISTS tmp_data
SELECT
src.id AS "id"
, src.name AS "name"
, 1 AS "source"
FROM
table_source1 AS src
WHERE
src.filter IN (1, 3, 5, 16, 21)
;
CREATE TEMPORARY TABLE IF NOT EXISTS tmp_data
SELECT
src.id AS "id"
, src.name AS "name"
, 2 AS "source"
FROM
table_source2 AS src
WHERE
src.filter IN (1, 3, 5, 16, 21)
;
SELECT * FROM tmp_data;
Is there a way to use a MySQL variable to store the filters ?
By doing something like that:
Code:
@filters = array{1, 3, 5, 16, 21};
...
WHERE
src.filter IN @filters
...
Otherwise I was thinking to use another temporary table with
the filters in it but I'm not sure that is the good solution:
Code:
DROP TEMPORARY TABLE IF EXISTS tmp_filters
CREATE TEMPORARY TABLE IF NOT EXISTS tmp_filters
(filter INT, PRIMARY KEY (filter))
SELECT 21 AS "filter";
INSERT INTO tmp_filters (filter) VALUES((1), (3), (5), (16), (21));
...
FROM
table_source2 AS src
JOIN tmp_filters AS flt ON (src.filter = ftl.filter)
;
...