2011-11-10 2 views
0

조사를 수행하고 로그 DB에 대해 쿼리를 작성합니다.MySQL 쿼리 지원 - 생략, 그룹화?

필자가 필요로하는 데이터를 가져 오기 위해 여러 테이블에 가입했지만 약간 정리하고 싶습니다.

이 쿼리는 모든 사용자와 계정에서 사용할 수있는 기능을 반환합니다.

가 '추가'및 '제거'

사용자 기능의 경우,이 개 상태가 '행동'이라는 칼럼 그들의입니다 : 여기

내가 그것을 청소하기 위해 할 노력하고있어입니다 '제거 된'동작이있는 경우 해당 사용자의 동일한 기능에 대해 '추가됨'으로 표시되는 행을 표시하지 않겠습니까

이것이 가능합니까? 여기

는 내가 지금까지 무엇을 가지고 : 어떤 지원을위한

select users.id as site_id, users.company_name, feature_log.featurecode, feature.minimum_account as feature_type, users.account_type as site_type, account_types.name as account, feature_log.action, feature_log.time 
from users 
inner join 
    feature_log 
    on users.id = feature_log.siteid 
inner join 
    feature 
    on feature_log.featurecode = feature.featurecode 
inner join account_types 
    on users.account_type_INC = account_types.id 
where feature.minimum_account != 0 
    AND feature.minimum_account > users.account_type 
    AND users.status = 'Y' 
    ORDER BY feature_log.time DESC 

감사합니다!

+0

은'feature_log' 테이블이 얼마나 큰을? 이 쿼리의 맥락에서 성능이 중요합니까? (종속 하위 쿼리 또는 왼쪽 조인을 사용하면 상당히 느릴 수 있습니다.) – Romain

+0

약 8K 행만 수행하면 조사가 중단 된 것처럼 성능이 중요하지 않습니다. – adamtor45

답변

1

그래서, 특정 사용자에 대한 어느 시점에서 "제거"된 "음소거"모든 기능이, 당신이 (왼쪽) 다음과 같은 하위 쿼리에 가입 추가 할 수 있습니다하기 위해 :

SELECT DISTINCT users.id as siteid, feature_log.featurecode, TRUE as mute_feature 
FROM users 
INNER JOIN feature_log ON (users.id = feature_log.siteid) 
WHERE action = 'removed' 

특정 시점에서 특정 사용자가 사용하지 않도록 설정 한 기능 목록입니다. 그런 다음 쿼리의 WHERE 절에, 당신과 같이 필터를 추가 할 것 :

AND NOT IFNULL(mute_feature, FALSE) 

을 본질적으로, 그것은으로 전체 쿼리를 가져올 것 :

select users.id as site_id, users.company_name, feature_log.featurecode, feature.minimum_account as feature_type, users.account_type as site_type, account_types.name as account, feature_log.action, feature_log.time 
from users 
inner join 
    feature_log 
    on users.id = feature_log.siteid 
left join (
    SELECT DISTINCT users.id as siteid, feature_log.featurecode, TRUE as mute_feature 
    FROM users 
    INNER JOIN feature_log ON (users.id = feature_log.siteid) 
    WHERE action = 'removed' 
) as muted_features ON (feature_log.siteid = muted_features.siteid AND feature_log.featurecode = muted_features.featurecode) 
inner join 
    feature 
    on feature_log.featurecode = feature.featurecode 
inner join account_types 
    on users.account_type_INC = account_types.id 
where feature.minimum_account != 0 
    AND feature.minimum_account > users.account_type 
    AND users.status = 'Y' 
    AND NOT IFNULL(mute_feature, FALSE) 
    ORDER BY feature_log.time DESC 
+1

로메 인, 경로를 교차하는 경우 맥주 한 잔 사 줄께! 고마워,이 완벽하게 일했다! – adamtor45

+1

@ adamtor45 나는 이것을 기억할 것이다 :) 어쨌든, 이것은 그렇게 어렵지 않았다 - 당신은 약간의 연습만으로 이것을 스스로 할 수있다. – Romain