组合2个MySQL查询 [英] Combine 2 MySQL queries
本文介绍了组合2个MySQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个查询
$mysqli->query("SELECT COUNT(*) from `login_log` where from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 WEEK);") or die($mysqli->error.__LINE__);
和同一个diff。它不是 1周,但 1个月
如何组合这两个并将它们分配给别名?
and the same one with 1 diff. it's not 1 WEEK , but 1 MONTH how can I combine those two and assign them to aliases ?
推荐答案
我将使用条件聚合:
SELECT SUM(from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 WEEK)),
SUM(from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 MONTH))
FROM `login_log`;
MySQL将布尔值视为整数, 1
为true, 0
为false。因此,使用 sum()
可以计算匹配值的数量。 (在其他数据库中,您将使用 case
执行类似的操作。)
MySQL treats boolean values as integers, with 1
being "true" and 0
being "false". So, using sum()
you can count the number of matching values. (In other databases, you would do something similar using case
.)
这篇关于组合2个MySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文