代码之家  ›  专栏  ›  技术社区  ›  potapuff

MySQL和PostgreSQL行子查询操作符

  •  0
  • potapuff  · 技术社区  · 6 年前

    我在阅读MySQL文档时有点困惑 MySQL documentation

     A row subquery is a subquery variant that returns a single row and can thus return more than one column value. Legal operators for row subquery comparisons are:
      =  >  <  >=  <=  <>  !=  <=>
    

    对于“=”文档提供了很好的解释:

    SELECT * FROM t1 WHERE (column1,column2) = (1,1);
    is same as:
    SELECT * FROM t1 WHERE column1 = 1 AND column2 = 1;
    

    create table example(a integer,b integer);
    insert into example values (1,1);
    insert into example values (1,2);
    insert into example values (2,1);
    select * from example where (a,b) > (1,1)
    a | b
    -----
    1 | 2
    2 | 1
    

    游乐场: http://www.sqlfiddle.com/#!9/88641/2

    附笔。:

    • Oracle失败,错误为“ORA-01796:此运算符不能用于列表”
    1 回复  |  直到 6 年前
        1
  •  0
  •   Community CDub    4 年前

    MySQL和PostgreSQL有相同的行为 postgres documentation

    对于<、<=、>和>=的情况,行元素将从左到右进行比较,一旦找到不相等或空的元素对,就会立即停止。如果这对元素中的任何一个为null,则行比较的结果为未知(null);否则,对这对元素的比较将确定结果。例如,ROW(1,2,NULL)<ROW(1,3,0)的结果是true,not NULL,因为没有考虑第三对元素。

    所以(a,b)=(c,d)计算为 a=c和b=d

    但是(a,b,)<(c,d)评估为 a<b或(a=c和b<c)