<i id='hfmGM'><tr id='hfmGM'><dt id='hfmGM'><q id='hfmGM'><span id='hfmGM'><b id='hfmGM'><form id='hfmGM'><ins id='hfmGM'></ins><ul id='hfmGM'></ul><sub id='hfmGM'></sub></form><legend id='hfmGM'></legend><bdo id='hfmGM'><pre id='hfmGM'><center id='hfmGM'></center></pre></bdo></b><th id='hfmGM'></th></span></q></dt></tr></i><div id='hfmGM'><tfoot id='hfmGM'></tfoot><dl id='hfmGM'><fieldset id='hfmGM'></fieldset></dl></div>
  • <tfoot id='hfmGM'></tfoot>

    1. <legend id='hfmGM'><style id='hfmGM'><dir id='hfmGM'><q id='hfmGM'></q></dir></style></legend>

      <small id='hfmGM'></small><noframes id='hfmGM'>

        <bdo id='hfmGM'></bdo><ul id='hfmGM'></ul>

      1. 使用 LEFT OUTER JOIN 检查相关行不存在的最佳方法是什么

        时间:2023-10-09

        <legend id='nUAxN'><style id='nUAxN'><dir id='nUAxN'><q id='nUAxN'></q></dir></style></legend>

      2. <i id='nUAxN'><tr id='nUAxN'><dt id='nUAxN'><q id='nUAxN'><span id='nUAxN'><b id='nUAxN'><form id='nUAxN'><ins id='nUAxN'></ins><ul id='nUAxN'></ul><sub id='nUAxN'></sub></form><legend id='nUAxN'></legend><bdo id='nUAxN'><pre id='nUAxN'><center id='nUAxN'></center></pre></bdo></b><th id='nUAxN'></th></span></q></dt></tr></i><div id='nUAxN'><tfoot id='nUAxN'></tfoot><dl id='nUAxN'><fieldset id='nUAxN'></fieldset></dl></div>
                  <bdo id='nUAxN'></bdo><ul id='nUAxN'></ul>
                • <small id='nUAxN'></small><noframes id='nUAxN'>

                  <tfoot id='nUAxN'></tfoot>
                    <tbody id='nUAxN'></tbody>
                  本文介绍了使用 LEFT OUTER JOIN 检查相关行不存在的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  使用 MySQL 5.x 我想有效地从表 X 中选择表 Y 中没有相关行满足某些条件的所有行,例如

                  Using MySQL 5.x I want to efficiently select all rows from table X where there is no related row in table Y satisfying some condition, e.g.

                  给我 X 中所有与 foo = bar 相关的 Y 不存在的记录

                  Give me all records in X where a related Y with foo = bar does NOT exist

                  SELECT count(id) FROM X
                  LEFT OUTER JOIN Y ON y.X_id = X.id AND y.foo = 'bar'
                  WHERE y....?
                  

                  据我所知,左外连接保证为左(第一个)表中的每一行生成一行——在本例中为 X——无论是否在连接表中找到了令人满意的行.我想要做的就是只选择那些没有找到行的行.

                  As I understand it, a left outer join is guaranteed to produce a row for each row in the left (first) table -- X in this case -- whether or not a satisfying row in the joined table was found. What I want to do is then select only those rows where no row was found.

                  在我看来,如果没有匹配的记录,y.X_id 应该是 NULL,但是这个测试似乎不起作用.y.X_id = 0 或 !y.X_id 也不是.

                  It seems to me that y.X_id should be NULL if there is no matching record, but this test doesn't seem to work. Nor does y.X_id = 0 or !y.X_id.

                  编辑:更正了多个回复指出的转录错误(ON 不是 AS).修正语法错误.

                  Edits: corrected transcription error (ON not AS) which was pointed out by several responses. Fixed grammatical error.

                  推荐答案

                  SELECT count(id) FROM X 
                  LEFT OUTER JOIN Y ON (y.X_id = X.id AND y.foo = 'bar')
                  WHERE y.X_id is null
                  

                  你已经接近了.

                  首先照常进行连接,然后选择 Y 中 not null 行实际上是 null 的所有行,因此您确定存在不匹配"" 而不仅仅是 Y 中的 null 值.

                  First do the join as normal, then select all rows for which a not null row in Y is in fact null, so you are sure there's a "no match" and not just a null value in Y.

                  还要注意您在查询中的拼写错误(已更正):

                  Also note the typo (since corrected) you made in the query:

                  LEFT OUTER JOIN Y AS
                  -- should be
                  LEFT OUTER JOIN Y ON
                  -- This however is allowed
                  LEFT OUTER JOIN table2 as Y ON ....
                  

                  这篇关于使用 LEFT OUTER JOIN 检查相关行不存在的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySQL UPDATE 与来自不同表的 SELECT SUM 下一篇:INNER JOIN 和 LEFT/RIGHT OUTER JOIN 的问题

                  相关文章

                    <bdo id='Td8eN'></bdo><ul id='Td8eN'></ul>
                  <tfoot id='Td8eN'></tfoot>

                  <legend id='Td8eN'><style id='Td8eN'><dir id='Td8eN'><q id='Td8eN'></q></dir></style></legend>
                  <i id='Td8eN'><tr id='Td8eN'><dt id='Td8eN'><q id='Td8eN'><span id='Td8eN'><b id='Td8eN'><form id='Td8eN'><ins id='Td8eN'></ins><ul id='Td8eN'></ul><sub id='Td8eN'></sub></form><legend id='Td8eN'></legend><bdo id='Td8eN'><pre id='Td8eN'><center id='Td8eN'></center></pre></bdo></b><th id='Td8eN'></th></span></q></dt></tr></i><div id='Td8eN'><tfoot id='Td8eN'></tfoot><dl id='Td8eN'><fieldset id='Td8eN'></fieldset></dl></div>
                • <small id='Td8eN'></small><noframes id='Td8eN'>