zoukankan      html  css  js  c++  java
  • A2-02-23.DML- MySQL INTERSECT

    转载自:http://www.mysqltutorial.org/mysql-intersect/

    MySQL INTERSECT

     

    Summary: in this tutorial, we will introduce you to the SQL INTERSECT operator and show you how to simulate the MySQL INTERSECT operator.

    Introduction to the SQL INTERSECT operator

    The INTERSECT operator is a set operator that returns only distinct rows of two queries or more queries.

    The following illustrates the syntax of the INTERSECT operator.

    The INTERSECT operator compares the result of two queries and returns the distinct rows that are output by both left and right queries.

    To use the INTERSECT operator for two queries, the following rules are applied:

    1. The order and the number of columns must be the same.
    2. The data types of the corresponding columns must be compatible.

    The following diagram illustrates the INTERSECT operator.

    MySQL INTERSECT

    The left query produces a result set of (1,2,3).

    The right query returns a result set of (2,3,4).

    The INTERSECT operator returns the distinct rows of both result sets which include (2,3).

    Unlike the UNION operator, the INTERSECT operator returns the intersection between two circles.

    Note that SQL standard has three set operators that include UNIONINTERSECT, and MINUS.

    MySQL INTERSECT simulation

    Unfortunately, MySQL does not support the INTERSECT operator. However, you can simulate the INTERSECT operator.

    Let’s create some sample data for the demonstration.

    The following statements create tables t1 and t2, and then insert data into both tables.

    The following query returns rows from the t1 table .

    The following query returns the rows from the t2 table:

    Simulate MySQL INTERSECT operator using DISTINCT operator and INNER JOIN clause.

    The following statement uses DISTINCT operator and INNER JOIN clause to return the distinct rows in both tables:

    How it works.

    1. The INNER JOIN clause returns rows from both left and right tables.
    2. The DISTINCT operator removes the duplicate rows.

    Simulate MySQL INTERSECT operator using IN operator and subquery

    The following statement uses the IN operator and a subquery to return the intersection of the two result sets.

    How it works.

    1. The subquery returns the first result set.
    2. The outer query uses the IN operator to select only values that are in the first result set. The DISTINCT operator ensures that only distinct values are selected.

    In this tutorial, you have learned a couple of ways to simulate the INTERSECT operator in MySQL.

  • 相关阅读:
    linux less-分屏上下翻页浏览文件内容
    linux tail-在屏幕上显示指定文件的末尾若干行
    linux cut-连接文件并打印到标准输出设备上
    linux od-输出文件的八进制、十六进制等格式编码的字节
    linux hexdump-显示文件十六进制格式
    linux whereis-查找二进制程序、代码等相关文件路径
    linux find-在指定目录下查找文件
    linux which-查找并显示给定命令的绝对路径
    linux diff3-比较3个文件不同的地方
    Datetimepicker实现秒钟选择下拉框
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9518940.html
Copyright © 2011-2022 走看看