我正在尝试从现有表中获取行(按列:p_id
,e_id
,ts
,data1
,data2
,{{ 1}})到临时表(由data3
,p_id
,e_id
索引),然后从该临时表中选择特定行作为结果。然后终止临时表(因为那是CREATE TEMPORARY的作用,对吗?)
我回顾了这两篇文章:
并且对于正确编写查询(我的查询未运行)感到困惑。
ts
答案 0 :(得分:2)
CREATE TEMPORARY TABLE IF NOT EXISTS
pet_temp ( INDEX(p_id, e_id, ts) )
AS (
SELECT p_id, e_id, ts, data1, data2, data3
FROM processes_history
WHERE e_id=4362 AND ts BETWEEN '2017-03-01' AND '2017-04-01'
) ;
SELECT p_id, e_id, ts, data1, data2, data3 FROM pet_temp WHERE p_id IN (11,22,33,44,55,66,77,88,99);
Drop temporary table if exists pet_temp ;
答案 1 :(得分:0)
CREATE TEMPORARY TABLE IF NOT EXISTS
pet_temp ( INDEX(p_id, e_id, ts) )
AS
SELECT p_id, e_id, ts, data1, data2, data3
FROM processes_history
WHERE e_id=4362 AND ts BETWEEN '2017-03-01' AND '2017-04-01';
SELECT p_id, e_id, ts, data1, data2, data3 FROM pet_temp WHERE p_id IN (11,22,33,44,55,66,77,88,99);
DROP TEMPORARY TABLE IF EXISTS pet_temp;
看起来你只需要删除第一组(),并在语句之间添加分号。
答案 2 :(得分:0)
你可以使用下面的查询,它会在表格中给出欲望:
CREATE TEMPORARY TABLE IF NOT EXISTS pet_temp (INDEX(p_id, e_id, ts))
SELECT p_id, e_id, ts, data1, data2, data3
FROM (
SELECT p_id, e_id, ts, data1, data2, data3
FROM processes_history
WHERE e_id=4362 AND ts BETWEEN '2017-03-01' AND '2017-04-01'
) x
WHERE p_id IN (11,22,33,44,55,66,77,88,99);
SELECT * FROM pet_temp;
不需要删除pet_temp表,一个会话关闭时会自动删除。
答案 3 :(得分:-1)
如果您以后没有计划使用临时表,为什么还需要临时表?
下一个不会做这个工作?
SELECT p_id, e_id, ts, data1, data2, data3 FROM processes_history WHERE e_id=4362 AND p_id IN (11,22,33,44,55,66,77,88,99) AND ts BETWEEN '2017-03-01' AND '2017-04-01';
如果我遗漏了某些内容,请纠正我