9, SQL Basic finishing (two tables connected Exists,join on,union)

Source: Internet
Author: User

The use of Exists

Select *from haha where exists (select *from bumen where Bumen.code = haha.bumen and bumen.name = ' Sales department ') and AGE>35

(Run method is query-by-clause)

Select Name,sex,age, (select name from bumen where Bumen.code = haha.bumen) as department from haha

Select Name,sex,age, (select name from bumen where Bumen.code = haha.bumen) as department, (select CEO from bumen where Bumen.code = Haha.bumen) as CEO from haha

A simple way to connect two tables:

1.

Select Haha.name,sex,age,bumen.name,ceo from Bumen,haha where haha.bumen = Bumen.code

2.join on (order can be reversed)

Select Haha.name,sex,age,bumen.name,ceo from haha

Join bumen on haha.bumen = Bumen.code

3.full usage of

INSERT into haha values (15, ' physical ', ' NV ', 34,5)

INSERT into bumen values (6, ' Security Department ', ' secure ', null,null)

Select Bumen.name,zhineng,ceo,haha.name,sex,age from haha

Full Join bumen on bumen.code = Haha.bumen

4 . use of left

In the case of no relationship, only all data from the table on the left side of the join is displayed, not the data from the right table

5 . The use of right

Ditto

(left and right are all sorted in the same way as all displayed data)

The use of Union

Connect the two columns, must meet the data type corresponding, with automatic deduplication function (according to pinyin or digital arrangement, to disrupt the original order)

select*from haha where code > 10

Union

select*from haha where code < 5

Select Name,bumen from haha where code > 10

Union

Select Ceo,code from Bumen

9, SQL Basic finishing (two tables connected Exists,join on,union)

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.