这是一个由Ryan Frank在forums.mysql.com上提出的问题,我也正在面对这个问题。COUNT(DISTINCT)子句中的WHERE子句
我已经在我的SELECT语句的开头如下:
SELECT accounts.id, accounts.company, accounts.first, accounts.last,
COUNT(DISTINCT accounts_log.login_time) AS visits,
COUNT(DISTINCT accounts_log.ip_address) AS visitors,
COUNT(DISTINCT documents_log.access_time) AS docs,
MAX(accounts_log.login_time) AS login_time
FROM accounts
这将返回我需要的所有变量;不过,我想将使用COUNT(DISTINCT)的变量限制在日期范围内。我无法在FROM子句之后使用WHERE子句。例如:
FROM accounts
WHERE accounts_log.login_time >='$search_from' AND accounts_log.login_time <='$search_to'
将无法正常工作,因为它不会给我像我需要的所有帐户。
我在寻找类似:
COUNT(DISTINCT accounts_log.login_time WHERE accounts_log.login_time >='$search_from' AND accounts_log.login_time <='$search_to') AS visits
附:我知道上述不起作用,并已用完语法选项。
您没有向我们展示整个查询。 'accounts_log'表来自哪里? –
假设你可以用帐户 – eyaler