Skip to content Skip to sidebar Skip to footer

Combine 2 Mysql Queries

I have this query SELECT COUNT(*) from `login_log` where from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL 1 WEEK); and the same one with 1 diff. it's not 1 WEEK , but 1 MONTH

Solution 1:

I would do this with conditional aggregation:

SELECTSUM(from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL1 WEEK)),
       SUM(from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL1MONTH))
FROM `login_log`;

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.)

Solution 2:

Use the where condition with one month internal and add the same where condition with one week internal as a Boolean column return.

I mean

Selectcount (*) all_in_month, (from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL1 WEEK)) as in_week from `login_log` where from_unixtime(`date`) >= DATE_SUB(NOW(), INTERVAL1 a MONTH) GROUPBY in_week;

P.s. haven't tested but afaik it should work

Solution 3:

Even though it's pretty tough to understand what you ask:

If you want them in the same column use OR

SELECTCOUNT(*) from'login_log'where from_unixtime('date') >= DATE_SUB(NOW(), INTERVAL1 WEEK) OR from_unixtime('date') >= DATE_SUB(NOW(), INTERVAL1MONTH) ;

If you don't want duplicate answers: use GROUP BY

Post a Comment for "Combine 2 Mysql Queries"