MYSQL data query. Multi-table subquery

1. Subquery overview

1. Sub query (Sub Query) or inner query (Inner Query), also known as nested query (Nested Query), is a query nested in the WHERE clause of other SQL queries.

 Subqueries are used to return the data required by the main query, or to further restrict the retrieved data.

 Subqueries can be used with operators such as =, <, >, >=, <=, IN, BETWEEN, etc. in SELECT, INSERT, UPDATE, and DELETE statements.

2. According to the results returned by the subquery:

A subquery returns a value (= > >= < <=)

Column subquery (subquery result is a column) (in, all, exists)

Row subquery (subquery result is one row)

Table subquery (the result of the subquery is multiple rows and multiple columns) (in)

3. Syntax format:

                select * from table 1 where column 1 = (select column 2 from table 2 [where condition]);    

Note: The names of column 1 and column 2 can be different, but the field values ​​are the same

2. Simple example:

The book management database has reader reader book borrowing data table reader table reader has reader ID number dzzh, name xm, gender xb, identity sf, phone number dhhm fields 

The book table book has barcode txm, book title sm, classification number flh, author zz, publisher cbs, publication date cbrq, price sj, collection category dclb, in library zk, currency bz fields

The borrowing table borrow has five fields: borrowing number jyid, reader ID number dzzh, barcode txm, borrowing date jyrq, and return date hsrq. When a reader borrows a book, add a record in the borrowing data table, set the return date to NULL, and set the zk field of the book to 0. When the reader returns the book, set the return date of the corresponding borrowing record to Today's date, and modify the zk field of the book to 1.

 question:

The first question is to query the reader ID number and name of the reader who has borrowed the book

The second question is to query the barcodes and titles of books that have not been borrowed

The third question is to query the reader ID number and name of the readers who have borrowed the same book as Sun Siwang, and arrange them in ascending order of the reader ID number

The fourth question is to query the titles of other books borrowed by readers who have borrowed the complete works of Li Bai in ascending order of titles

code:

1.
select dzzh,xm from reader where reader.dzzh in (select dzzh from borrow);

2.
select txm,sm from book where book.txm not in (select txm from borrow);

3.
select dzzh,xm from reader where reader.dzzh in (select dzzh from borrow 
where txm in (select txm from borrow 
where borrow.dzzh=(select dzzh from reader 
where xm='孙思旺'))) 
and dzzh!='006' order by dzzh asc;

4.
select sm from book where book.txm in (select txm from borrow
where borrow.dzzh in (select dzzh from borrow 
where borrow.txm=(select txm from book where sm='李白全集')))
and sm!='李白全集' order by sm asc;

Guess you like

Origin blog.csdn.net/m0_62428181/article/details/127266809