'IN / ALL / ANY'子查詢中的未知列

[英]Unknown Column in 'IN/ALL/ANY' subquery


I've got 2 tables: members and member_logs.

我有2個表:members和member_logs。

Members can belong to groups, which are in the members table. Given a date range and a group I'm trying to figure out how to get the 10 days with the highest number of successful logins. What I have so far is a massive nest of subquery terror.

成員可以屬於成員表中的組。給定一個日期范圍和一個組,我試圖弄清楚如何獲得成功登錄次數最多的10天。到目前為止,我所擁有的是一個龐大的子查詢恐怖巢。

SELECT count(member_id) AS `num_users`,
DATE_FORMAT(`login_date`,'%Y-%m-%d') AS `reg_date` 
FROM member_logs 
WHERE `login_success` = 1 
and `reg_date` IN 
    (SELECT DISTINCT DATE_FORMAT(`login_date`,'%Y-%m-%d') AS `reg_date` 
     FROM member_logs 
     WHERE `login_success` = 1 
     and (DATE_FORMAT(`login_date`,'%Y-%m-%d') BETWEEN '2012-02-25' and '2014-03-04'))
and `member_id` IN 
    (SELECT `member_id` 
     FROM members 
     WHERE `group_id` = 'XXXXXXX' 
     and `deleted` = 0) 
ORDER BY `num_users` desc 
LIMIT 0, 10

As far as I understand what is happening is that the WHERE clause is evaluating before the subqueries generate, and that I also should be using joins. If anyone can help me out or point me in the right direction that would be incredible.

據我所知,發生的事情是WHERE子句在子查詢生成之前進行評估,我也應該使用連接。如果有人能幫助我,或指出我正確的方向,那將是不可思議的。

EDIT: Limit was wrong, fixed it

編輯:限制是錯誤的,修復它

3 个解决方案

#1


3  

The first subquery is totally unnecessary because you can filter by dates directly in the current table member_logs. I also prefer a JOIN for the second subquery. Then what you are missing is grouping by date (day).

第一個子查詢完全沒必要,因為您可以直接在當前表member_logs中按日期過濾。我也更喜歡第二個子查詢的JOIN。然后你缺少的是按日期(日)分組。

A query like the following one (not tested) will do the job you want:

像下面這樣的查詢(未經測試)將完成您想要的工作:

SELECT COUNT(ml.member_id) AS `num_users`, 
  DATE_FORMAT(`login_date`,'%Y-%m-%d') AS `reg_date` 
FROM member_logs ml
INNER JOIN members m ON ml.member_id = m.member_id
WHERE `login_success` = 1 
  AND DATE_FORMAT(`login_date`,'%Y-%m-%d') BETWEEN '2012-02-25' AND '2014-03-04'
  AND `group_id` = 'XXXXXXX' 
  AND `deleted` = 0 
GROUP BY `reg_date`
ORDER BY `num_users` desc 
LIMIT 10

#2


0  

SELECT count(member_id) AS `num_users`,
DATE_FORMAT(`login_date`,'%Y-%m-%d') AS `reg_date` 
FROM member_logs 
WHERE `login_success` = 1 
and `login_date` IN 
    (SELECT `login_date` 
     FROM member_logs 
     WHERE `login_success` = 1 
     and (DATE_FORMAT(`login_date`,'%Y-%m-%d') BETWEEN '2012-02-25' and '2014-03-04'))
and `member_id` IN 
    (SELECT `member_id` 
     FROM members 
     WHERE `group_id` = 'XXXXXXX' 
     and `deleted` = 0) 
Group by `login_date`
ORDER BY `num_users` desc 
LIMIT 0, 10

#3


0  

As a slightly more index friendly version of the previous answers;

作為以前答案的稍微索引友好的版本;

To make the query index friendly, you shouldn't do per row calculations in the search conditions. This query removes the per row calculation of the string format date in the WHERE, so it should be faster if there are many rows to eliminate by date range;

要使查詢索引友好,不應在搜索條件中按行計算。此查詢刪除了WHERE中字符串格式日期的每行計算,因此如果按日期范圍排除許多行,它應該更快;

SELECT COUNT(*) num_users, DATE(login_date) reg_date
FROM member_logs JOIN members ON member_logs.member_id = members.member_id
WHERE login_success = 1 AND group_id = 'XXX' AND deleted = 0 
  AND login_date >= '2012-02-25' 
  AND login_date < DATE_ADD('2014-03-04', INTERVAL 1 DAY)
GROUP BY DATE(login_date)
ORDER BY num_users DESC 
LIMIT 10

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/03/04/72fab86d8701c1dad259e8223c7556a3.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com