以1个查询运行多个mysql查询

时间:2018-01-26 11:13:25

标签: mysql

我试图获取3个sql查询并将它们插入到1个表中而不获取空值并使用数字分组,以便不在同一列中获得重复的数字。

我遇到的问题是运行查询1会给我留下一堆空数据值 并且运行查询2不会对数字进行分组,导致数千行数最多只能达到100

QUERY 1

insert into table ( number)
select number as 1day from table where date = CURDATE() - interval 1day group by number
 insert into table ( number)
select number as 2day from table where date = CURDATE() - interval 1day group by number
insert into table ( number)
select number as 7day from table where date = CURDATE() - interval 1day group by number

所以我尝试运行

QUERY 2

insert into table (number,number,number)
select 
*
 from
(select number as 1day from test.test where date = curdate() - interval 1 day group by 
 number) as 1day,
(select number as 2day from test.test where date > curdate() - interval 2 day group by 
 number) as 2day,
 (select number as 7day from test.test where date > curdate() - interval 7 day group 
 by number) as 7day;

1st query 2nd query

2 个答案:

答案 0 :(得分:0)

尝试以下方法:

insert into table (number,number,number)
select 
table.1day,table.2day,table.7day
 from
((select number from test.test where date = curdate() - interval 1 day group by 
 number) as 1day,
(select numberfrom test.test where date > curdate() - interval 2 day group by 
 number) as 2day,
 (select number from test.test where date > curdate() - interval 7 day group 
 by number) as 7day) as table

答案 1 :(得分:0)

select (case one.number when two.number then null else one.number end) as '1day',(case two.number <= third.number when true then (case one.number = two.number when true then null else two.number end)  else (case one.number = two.number when false then null else two.number end) end) as '2day',(case (third.number < one.number and third.number = two.number) when true then null else third.number end) as '7day'
from (
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='2day' order by x.number) as two,
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='1day' order by x.number) as one,
(select x.number
from (
(select number,'1day' as 'type' from testtable where date = curdate() - interval 1 day group by number)
union all
(select number,'2day' as 'type' from testtable where date > curdate() - interval 2 day group by number)
union all
(select number,'7day' as 'type' from testtable where date > curdate() - interval 7 day group by number)) as x
where x.type='7day' order by x.number) as third
)
where ((one.number = two.number) or  (one.number is null or two.number is null)) or
((third.number = two.number) or  (two.number is null or third.number is null))