MySQL intersection Implementation Method

Source: Internet
Author: User

1

How to implement the following SQL query in MySQL
(Select S. Name
From student s, transcript t
Where S. studid = T. studid and T. crscode = 'cs305 ')
Intersect
(Select S. Name
From student s, transcript t
Where S. studid = T. studid and T. crscode = 'cs315 ')

Thank you ~

Solution:

Intersection

Select A. * From
(
Select S. Name
From Student s, transcript t
Where S. studid = T. studid And T. crscode = ' Cs305 '
) As A
Cross Join
(
Select S. Name
From Student s, transcript t
Where S. studid = T. studid And T. crscode = ' Cs315 '
) As B On A. Name = B. Name;

2.

 

Select * from (
Select distinct col1 from T1 where...
Union all
Select distinct col1 from T1 where...
) As TBL
Group by TBL. col1 having count (*) = 2
3.

Intersection:

Select * From Table1 as a join Table2 as B on A. Name = B. Name

Example:

Table:
Fielda
001
002
003

Table B:
Fielda
001
002
003
004

Ask how to obtain the following result set, that is, the intersection of Rows A and B.
Fielda
001
002
003

Answer: select a. fielda from a inner join B on A. fielda = B. fielda

Difference set:

Not in indicates the difference set
Select * From Table1 where name not in (Select name from table2)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.