Mysql 将两个查询放入同一个表(按日期分组)
我正在用MySQL数据库尝试我的第一个select语句,当你不知道用谷歌搜索什么的时候,这非常困难 这就是我要做的,我有两个问题:Mysql 将两个查询放入同一个表(按日期分组),mysql,sql,Mysql,Sql,我正在用MySQL数据库尝试我的第一个select语句,当你不知道用谷歌搜索什么的时候,这非常困难 这就是我要做的,我有两个问题: select date(server_time) as d, count(*) as x from table_A, table_B where date(server_time) >= '2015-07-17' and table_A.id = table_B.id and idname is not null and idurl is not
select date(server_time) as d, count(*) as x
from table_A, table_B
where date(server_time) >= '2015-07-17'
and table_A.id = table_B.id
and idname is not null
and idurl is not null
group by date(server_time)
及
我试图实现的是将两个计数列与日期结合在一起:
d x y
-------------|-----|-------
2015-07-17 | 3 | 20
2015-07-18 | 2 | 50
2015-07-19 | 7 | 10
-------------|-----|-------
我一直在玩弄UNION、SELECT count等,但运气不好。请定义x和y,并发布相关的表结构创建表代码。如果输出两个查询的实际结果,则更容易获得有用的答案。也考虑使用。顺便说一句,我打赌UNION ALL=d第一个查询得到一个包含d列和x列的表,第二个包含d列和y列。。我只想把它们合并在一起。@Kathi typo,现在试试谢谢,你忘了在内部选择中也包括id,那么它就像一个符咒!
SELECT date(server_time) AS d,
count(*) AS x,
(SELECT y
FROM (SELECT date(server_time) AS d,
id,
count(*) AS y
FROM table_A
WHERE date(sever_time) >= '2015-07-17'
GROUP BY date(server_time)) sub
WHERE SUB.id = a.id) AS y
FROM table_A a
INNER JOIN table_B b ON a.id = b.id
WHERE date(server_time) >= '2015-07-17'
AND idname IS NOT NULL
AND idurl IS NOT NULL
GROUP BY date(server_time)
SELECT date(server_time) AS d,
count(*) AS x,
(SELECT y
FROM (SELECT date(server_time) AS d,
id,
count(*) AS y
FROM table_A
WHERE date(sever_time) >= '2015-07-17'
GROUP BY date(server_time)) sub
WHERE SUB.id = a.id) AS y
FROM table_A a
INNER JOIN table_B b ON a.id = b.id
WHERE date(server_time) >= '2015-07-17'
AND idname IS NOT NULL
AND idurl IS NOT NULL
GROUP BY date(server_time)