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

        <legend id='rDB7H'><style id='rDB7H'><dir id='rDB7H'><q id='rDB7H'></q></dir></style></legend>
          <bdo id='rDB7H'></bdo><ul id='rDB7H'></ul>

        仅当值不存在时才返回行

        时间:2023-08-20

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

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

                  <tfoot id='INqxG'></tfoot>
                1. 本文介绍了仅当值不存在时才返回行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有 2 张桌子 - reservation:

                  I have 2 tables - reservation:

                     id  | some_other_column
                     ----+------------------
                     1   | value
                     2   | value
                     3   | value
                  

                  第二个表 - reservation_log:

                     id  | reservation_id | change_type
                     ----+----------------+-------------
                     1   | 1              | create
                     2   | 2              | create
                     3   | 3              | create
                     4   | 1              | cancel
                     5   | 2              | cancel
                  

                  我只需要选择未取消的预订(在本例中仅为 ID 3).我可以使用简单的 WHERE change_type = cancel 条件轻松选择取消,但我正在努力解决未取消的问题,因为简单的 WHERE 在这里不起作用.

                  I need to select only reservations NOT cancelled (it is only ID 3 in this example). I can easily select cancelled with a simple WHERE change_type = cancel condition, but I'm struggling with NOT cancelled, since the simple WHERE doesn't work here.

                  推荐答案

                  SELECT *
                  FROM reservation
                  WHERE id NOT IN (select reservation_id
                                   FROM reservation_log
                                   WHERE change_type = 'cancel')
                  

                  或:

                  SELECT r.*
                  FROM reservation r
                  LEFT JOIN reservation_log l ON r.id = l.reservation_id AND l.change_type = 'cancel'
                  WHERE l.id IS NULL
                  

                  第一个版本更直观,但我认为第二个版本通常会获得更好的性能(假设您在连接中使用的列上有索引).

                  The first version is more intuitive, but I think the second version usually gets better performance (assuming you have indexes on the columns used in the join).

                  第二个版本有效,因为 LEFT JOIN 为第一个表中的所有行返回一行.当 ON 条件成功时,这些行将包含第二个表中的列,就像 INNER JOIN 一样.当条件失败时,返回的行将包含第二个表中所有列的 NULL.WHERE l.id IS NULL 测试然后匹配这些行,因此它会找到表之间不匹配的所有行.

                  The second version works because LEFT JOIN returns a row for all rows in the first table. When the ON condition succeeds, those rows will include the columns from the second table, just like INNER JOIN. When the condition fails, the returned row will contain NULL for all the columns in the second table. The WHERE l.id IS NULL test then matches those rows, so it finds all the rows that don't have a match between the tables.

                  这篇关于仅当值不存在时才返回行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何过滤具有多次通过关系的 SQL 结果 下一篇:MyISAM 与 InnoDB

                  相关文章

                  最新文章

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

                3. <small id='pZjbN'></small><noframes id='pZjbN'>

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

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