在一列中选择具有相等值的行,但不是那些单独的行

时间:2017-11-15 11:10:24

标签: mysql sql select

这是我的SQLfiddle:http://sqlfiddle.com/#!9/6a335/1/0

enter image description here

我想要的结果与图片相同但没有2个红色行,因为对于id_association_mots_idM 476和478,表格中没有英文译文。

我尝试了许多请求和许多方法。 是否可能,如果可以,怎么样?

感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

如果您希望将来可以维护SQL,那么您真的必须开始格式化和缩进SQL

以下查询排除红色行:

SELECT 
  ass_m_m.id_association_mots_idM, 
  ass_m_m.id_vocab_idM, 
  v_a_m.categorie_v , 
  v_a_m.mot_v 

FROM 
`association_mots_id_mot` AS ass_m_m
INNER JOIN
`association_mots_login` AS ass_m_l
ON 
  ass_m_m.id_association_mots_idM = ass_m_l.id_association_mots AND 
  ass_m_l.id_login_mL = '1'

INNER JOIN 
`vocabulaire_a_memoriser` AS v_a_m 
ON
  ass_m_m.id_vocab_idM = v_a_m.id_vocab AND 
  (v_a_m.categorie_v = 'langue_francais' OR v_a_m.categorie_v = 'langue_anglais')

INNER JOIN
(
  SELECT 
    ass_m_m.id_association_mots_idM 
  FROM 
    `association_mots_id_mot` AS ass_m_m
    INNER JOIN 
    `vocabulaire_a_memoriser` AS v_a_m 
    ON
      ass_m_m.id_vocab_idM = v_a_m.id_vocab AND 
      (v_a_m.categorie_v = 'langue_francais' OR v_a_m.categorie_v = 'langue_anglais')
   GROUP BY
     ass_m_m.id_association_mots_idM
   HAVING COUNT(distinct categorie_v) = 2  
) wanted
ON
  ass_m_m.id_association_mots_idM = wanted.id_association_mots_idM

我已将查询转换为ANSI连接语法 - 请避免使用旧的SELECT * FROM a,b,c,d WHERE a.id=b.id ...语法

查询的顶部是您的查询(转换后的连接语法,并且缩进布局,以便我们有机会看到发生了什么)

完成这项工作的是这个子查询:

(
  SELECT 
    ass_m_m.id_association_mots_idM 
  FROM 
    `association_mots_id_mot` AS ass_m_m
    INNER JOIN 
    `vocabulaire_a_memoriser` AS v_a_m 
    ON
      ass_m_m.id_vocab_idM = v_a_m.id_vocab AND 
      (v_a_m.categorie_v = 'langue_francais' OR v_a_m.categorie_v = 'langue_anglais')
   GROUP BY
     ass_m_m.id_association_mots_idM
   HAVING COUNT(distinct categorie_v) = 2  
) wanted

它将ass_m_m.id_association_mots_idM 2,2,5,5,5,476,478列表简化为2,5,因为它们是ass_m_m.id_association_mots_idM只有COUNT(DISTINCT language)的唯一2,5 - 即他们&#39} ;是唯一出现2个不同语言值的行

然后,因为此查询的输出在加入main时只是isdigit(),所以它将主查询限制为iass_m_m.id_association_mots_idM为2或5的行

答案 1 :(得分:0)

这是你想要的吗?

select t.*
from t
where exists (select 1
              from t t2
              where t2.id_association_mots_idm = t.id_association_mots_idm and
                    t2.category_v = 'langue_anglais'
             );

答案 2 :(得分:0)

以下查询排除了RED行。

SELECT v_all.id_association_mots_idM,
       v_all.id_vocab_idM,
       v_all.id_vocab_idM,
       v_all.mot_v
  FROM (SELECT  ass_m_m.id_association_mots_idM, 
                ass_m_m.id_vocab_idM, 
                v_a_m.categorie_v , 
                v_a_m.mot_v 
        FROM  `association_mots_login` AS ass_m_l,  
              `association_mots_id_mot` AS ass_m_m, 
              `vocabulaire_a_memoriser` AS v_a_m 
        WHERE ass_m_l.id_association_mots  = ass_m_m.id_association_mots_idM
          AND ass_m_l.id_login_mL = '1'
          AND ass_m_m.id_vocab_idM = v_a_m.id_vocab 
          AND v_a_m.categorie_v IN ('langue_francais', 'langue_anglais')) v_all,
        (SELECT DISTINCT ass_m_m.id_association_mots_idM
          FROM  `association_mots_login` AS ass_m_l,  
                `association_mots_id_mot` AS ass_m_m, 
                `vocabulaire_a_memoriser` AS v_a_m 
          WHERE ass_m_l.id_association_mots  = ass_m_m.id_association_mots_idM
            AND ass_m_l.id_login_mL = '1'
            AND ass_m_m.id_vocab_idM = v_a_m.id_vocab 
            AND v_a_m.categorie_v = 'langue_anglais') v_eng 
WHERE v_all.id_association_mots_idM = v_eng.id_association_mots_idM        ;

<强>解释

内联视图v_all与您拥有的相同。获取langue_francais和langue_anglais的所有记录。内联视图v_eng仅获取英文中有traduction的记录,即langue_anglais。 内部加入2个视图将为您提供英语traduction的记录。

希望这有帮助!