MySQL Database 8-sub-query, federated query

Source: Internet
Author: User

A subquery that uses the IN keyword

Question: The query game type is the ' Chess class ' score information for the game

-Game type information is not included in the Games score table

Idea one: Using link query

Idea two: In two steps, first find so ' chess class ' game number, and then this group number for query based on the completion of the query

SELECT * from scores where Gno in (select Gno from game where gtype = ' board ')

Example: Query the player QQ without participating in number 5th game

Select User_qq from the users where user_qq not in (select User_qq from scores where gno=5)

Two sub-queries using the EXISTS keyword

Example: If there is a nickname ' Monkey King ', then query the data in the score table

SELECT * from scores where exists (SELECT * from users user_name = ' Monkey King ')

Three joint query

Select _statement Union[all] select_statement [Union[all] SELECT_STATEMENT][...N]

function and characteristic: can connect the result set of multiple query statements vertically as one

All keywords can display all the data (that is, duplicates are also displayed)

The number of columns is compatible with the type

Select user_name from Users

Union

Select Gname from Games

Example: Query the player table for all female players and birthdays are empty players

SELECT * from users where user_sex= ' women '

Union

SELECT * from users where user_birthday is null

<<=====>> SELECT * from users where user_sex= ' female ' or select * from the users where User_birthday is null

Example: Query QQ number is ' 12302 ' of the player all scores and calculate the total score and average score, and display to the same result set

Select User_qq,gno,score from scores where user_qq= ' 12302 ' union ALL select ' Total score ', ', sum (score) from scores union ALL Sele CT ' average score ', ', AVG (score) from scores where user_qq= ' 12302 '

MySQL Database 8-subquery, federated query

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.