我想创建一个视图来显示来自这3个表的计数数据,其中有条件",有些"没有where条件"。
我第一次计算两次COUNT(e.user_id)的地方没有任何WHERE子句第二次计算" WHERE e.reg_date = CURDATE()"
对于第1部分,它不起作用,当我想获得没有任何where子句的user_id的总数时,它默认采取" WHERE e.reg_date = CURDATE()"对于这两种情况。
以下是代码:
CREATE OR REPLACE VIEW admin_summary AS (
SELECT COUNT(e.user_id) AS total_register_user, COUNT(e.user_id) AS register_today, COUNT(d.pr_status) AS total_pending_payment , COUNT(f.status) AS total_pending_account, COUNT(g.time) AS failed_login_today
FROM client e, payment d, masterbroker f, login_attempts g
WHERE e.reg_date = CURDATE() AND d.pr_status = 'pending' AND f.status ='pending' AND g.time = CURDATE()
);
更新1:数据记录: 注册用户总数2(所有时间) 立即注册:0 待付款总额:1 总待定帐户:1 登录失败:1
更新3:
由于数据类型混乱,我将登录尝试时间更改为datetime。
更新4:使用调试结果 似乎没有条件它在某些时候起作用。但我需要Where Where条件。 这是没有where子句的结果:
SELECT COUNT(e.user_id) AS total_register_user , SUM( CASE WHEN
date(e.reg_date) = CURDATE() THEN 1 ELSE 0 END) AS register_today ,
COUNT(d.pr_status) AS total_pending_payment ,
COUNT(f.status) AS total_pending_account ,
COUNT(g.time) AS failed_login_today
from client e INNER JOIN payment d on e.user_id = d.user_id
INNER JOIN masterbroker f on e.user_id = f.user_id
INNER JOIN login_attempts g on e.user_id = g.user_id
更新5:有关唯一索引的更多信息(如果需要)。 User_id是客户端表的PK& forgien key for other 3 table,payment,login_attempts& masterbroker。我有以下独特的索引:
masterbrokerindex ON masterbroker(user_id,accountno, broker_id);
paymentindex ON payment(user_id,pr_rqtime);
login_attempts_index ON login_attempts (user_id,ip);
答案 0 :(得分:2)
只关注问题的相关部分,获得两个不同数量的相同数据 在不同的条件下你应该使用自联接以两种不同的方式使用tabl
SELECT
COUNT(e1.user_id) AS total_register_user
, COUNT(e2.user_id) AS register_today
from client e1
left join client e2 on e1.user_id = e2.user_id and e2.reg_date = CURDATE();
....
或避免连接,您可以使用CASE WHEN过滤e.reg_date
SELECT
COUNT(e.user_id) AS total_register_user
, SUM( CASE WHEN e.reg_date = CURDATE() THEN 1 ELSE 0 END) AS register_today
from client e
.....
在您的查询中,表之间没有关系条件。这很不寻常......
并根据您的日期列数据类型(假设e.reg_date是日期时间而g.time是unix时间戳)
CREATE OR REPLACE VIEW admin_summary AS (
SELECT
COUNT(e.user_id) AS total_register_user
, SUM( CASE WHEN date(e.reg_date) = CURDATE() THEN 1 ELSE 0 END) AS register_today
, COUNT(d.pr_status) AS total_pending_payment
, COUNT(f.status) AS total_pending_account
, COUNT(g.time) AS failed_login_today
from client e, payment d, masterbroker f, login_attempts g
WHERE date(e.reg_date) = CURDATE()
AND d.pr_status = 'pending'
AND f.status ='pending'
AND date(g.time) = CURDATE()
);
表之间的连接尝试使用
使用左连接中的位置作为内部连接,因此where条件应该对相关的粘土中的条款具有相应性
CREATE OR REPLACE VIEW admin_summary AS (
SELECT
COUNT(e.user_id) AS total_register_user
, SUM( CASE WHEN date(e.reg_date) = CURDATE() THEN 1 ELSE 0 END) AS register_today
, COUNT(d.pr_status) AS total_pending_payment
, COUNT(f.status) AS total_pending_account
, COUNT(g.time) AS failed_login_today
from client e
LEFT JOIN payment d on e.user_id = d.user_id
AND date(e.reg_date) = CURDATE()
AND d.pr_status = 'pending'
LEFT JOIN masterbroker f on e.user_id = f.user_id
AND f.status ='pending'
LEFT JOIN login_attempts g on e.user_id = g.user_id
AND date(g.time) = CURDATE()
);
如果您不需要在客户端加入付款,可以尝试
CREATE OR REPLACE VIEW admin_summary AS (
SELECT
COUNT(e.user_id) AS total_register_user
, SUM( CASE WHEN date(e.reg_date) = CURDATE() THEN 1 ELSE 0 END) AS register_today
, COUNT(d.pr_status) AS total_pending_payment
, COUNT(f.status) AS total_pending_account
, COUNT(g.time) AS failed_login_today
from client e
LEFT JOIN payment d ON d.pr_status = 'pending'
LEFT JOIN masterbroker f on e.user_id = f.user_id
AND f.status ='pending'
LEFT JOIN login_attempts g on e.user_id = g.user_id
WHERE date(e.reg_date) = CURDATE()
);
最后..尝试在没有任何连接的情况下计算pending_payment
CREATE OR REPLACE VIEW admin_summary AS (
SELECT
COUNT(e.user_id) AS total_register_user
, SUM( CASE WHEN date(e.reg_date) = CURDATE() THEN 1 ELSE 0 END) AS register_today
, (select COUNT(pr_status) from payment where pr_status = 'pending' ) AS total_pending_payment
, COUNT(f.status) AS total_pending_account
, COUNT(g.time) AS failed_login_today
from client e
LEFT JOIN masterbroker f on e.user_id = f.user_id
AND f.status ='pending'
LEFT JOIN login_attempts g on e.user_id = g.user_id
AND date(g.time) = CURDATE()
);