Query of remaining bibliography (1)

Source: Internet
Author: User

There are the following basic tables:

You must complete the following query:

(1) list the remaining titles and quantity

(2) list all titles and the number of remaining titles

Solution:

(1 ):

Select title, count (title) as available
From book
Where id not in
(Select bookid
From borrow)
Group by title;

Query results:

(2 ):

Select title, count (ID)-count (userid) as remain
From book left join borrow on book. ID = borrow. bookid
Group by title;

Query results:

It can be seen that the difference from (1) is that there are no remaining titles listed.

Note:

1, (2) the left Outer Join is used. The effect is to keep the left side of the join without losing entries, such:

Select *
From book left join borrow on book. ID = borrow. bookid;
Query results:

2. Count (userid) does not count null values.

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.