Home Database Mysql Tutorial MySQl心得4--3--数据库查询3--from字句_MySQL

MySQl心得4--3--数据库查询3--from字句_MySQL

Jun 01, 2016 pm 01:42 PM
Database query surface

bitsCN.com  1.from 字句 SELECT的查询对象由FROM子句指定,其格式为:   FROM 表名1 [ , 表名2] … 其中,表名: tbl_name [ [AS] 表别名 ] [{USE | ignore | force} index (key_list)]                      /*查询表*/ | join_table                /*连接表*/ 说明:表名指出了要查询的表或视图。 ●  表别名主要用在相关子查询及连接查询中。如果FROM子句指定了表别名,这条SELECT语句中的其他子句都必须使用表别名来代替原始的表名。当同一个表在SELECT语句中多次被提到的时候,就必须要使用表别名来加以区分。 ●  {USE | IGNORE | FORCE} INDEX:USE INDEX告知MySQL选择一个索引来查找表中的行,IGNORE INDEX告知MySQL不要使用某些特定的索引,FORCE INDEX的作用接近USE INDEX(key_list),只有当无法使用一个给定的索引来查找表中的行时,才使用表扫描。 2.表名中可以包含一个或多个表:   ●   引用一个表: 可以用两种方式引用一个表,第一种方式是使用USE语句让一个数据库成为当前数据库,在这种情况下,如果在FROM子句中指定表名,则该表应该属于当前数据库。第二种方式是指定的时候在表名前带上表所属数据库的名字。例如,假设当前数据库是db1,现在要显示数据库db2里的表tb的内容,使用如下语句: SELECT  *  FROM db2.tb; 当然,在SELECT关键字后指定列名的时候也可以在列名前带上所属数据库和表的名字,但是一般来说,如果选择的字段在各表中是唯一的,就没有必要去特别指定。 ●   引用多个表: 如果要在不同表中查询数据,则必须在FROM子句中指定多个表。指定多个表时就要使用到连接。当不同列的数据组合到一个表中叫做表的连接。 3.连接的方式有以下三种。  高级查询  (多表查询:数据源是多个表) 做多表连接:要找到连接的条件,2个表的公共字段(属性) 分析:1.考虑数据源  :xs:学号  姓名; xs_kc:课程号  成绩               2.连接条件:两表的学号相等   xs.学号=xs_kc.学号 当查看的列在多个表中都存在的话,必须在字段名前加上表名进行限制。 1).  全连接 连接的第一种方式是将各个表用逗号分隔,这样就指定了全连接。FROM子句产生的中间结果是一个新表,新表是每个表的每行都与其他表中的每行交叉以产生所有可能的组合,列包含了所有表中出现的列,也就是笛卡儿积。这样连接表潜在地产生数量非常大的行,因为可能得到的行数为每个表中行数之积。在这样的情形下,通常要使用WHERE子句设定条件来将结果集减少为易于管理的大小,这样的连接即为等值连接。 例: 查找XSCJ数据库中所有学生选过的课程名和课程号。 SELECT DISTINCT KC.课程名,XS_KC.课程号    FROM KC, XS_KC  WHERE KC.课程号=XS_KC.课程号; 2). JOIN连接 语法格式如下:   表名1 inner join 表名2 [join_condition]  | 表名1{ left | right } [outer]JOIN表名2 join_condition  | 表名1 natural [ {RIGHT |LEFT} [OUTER] ] JOIN表名2  | 表名1 cross join 表名2 [join_condition]   | 表名1 straight_join表名2 [ON condition_exp] 其中,join_condition 代表:  ON 连接条件 | using(column_list) 使用JOIN关键字的连接主要分为三种: A.内连接(指定了inner关键字的连接是内连接)。     内连接是系统默认的,可以省略INNER关键字。使用内连接后,FROM子句中ON条件主要用来连接表,其他并不属于连接表的条件可以使用WHERE子句来指定。 作为特例,可以将一个表与它自身进行连接,称为自连接。若要在一个表中查找具有相同列值的行,则可以使用自连接。使用自连接时需为表指定两个别名,且对所有列的引用均要用别名限定。 例:查找XSCJ数据库中课程不同、成绩相同的学生的学号、课程号和成绩。 SELECT a.学号,a.课程号,b.课程号,a.成绩  FROM XS_KC AS a  JOIN  XS_KC AS b    ON a.成绩=b.成绩 AND a.学号=b.学号 ANDa.课程号!=b.课程号; 如果要连接的表中有列名相同,并且连接的条件就是列名相等,那么ON条件也可以换成USING子句。

USING(column_list)子句用于为一系列的列进行命名。这些列必须同时在两个表中存在。其中column_list为两表中相同的列名。 例: 查找KC表中所有学生选过的课程名。 SELECT 课程名  FROM KC INNER JOIN XS_KC  USING (课程号); 说明:查询的结果为XS_KC表中所有出现的课程号对应的课程名。 B.外连接(指定了OUTER关键字的连接为外连接。) 外连接包括: ●   左外连接(LEFT OUTER JOIN):结果表中除了匹配行外,还包括左表有的但右表中不匹配的行,对于这样的行,从右表被选择的列设置为NULL。 ●   右外连接(RIGHT OUTERJOIN):结果表中除了匹配行外,还包括右表有的但左表中不匹配的行,对于这样的行,从左表被选择的列设置为NULL。 ●   自然连接(NATURAL JOIN):自然连接还有自然左外连接(NATURAL LEFT OUTER JOIN)和自然右外连接(NATURALRIGHT OUTER JOIN)。NATURAL JOIN的语义定义与使用了ON条件的INNER JOIN相同。 其中的OUTER关键字均可省略。 例: 查找所有学生情况及他们选修的课程号,若学生未选修任何课,也要包括其情况。 SELECT XS.* , 课程号     FROM XS LEFT OUTER JOIN XS_KC ON XS.学号 =XS_KC.学号; 说明:若本例不使用LEFT OUTER JOIN,则结果中不会包含未选任何课程的学生信息。使用了左外连接后,本例结果中返回的行中有未选任何课程的学生信息,相应行的课程号字段值为NULL。    例: 使用自然连接实现例4.22中相同的结果。 SELECT DISTINCT 课程名, XS_KC.课程号      FROM KC NATURAL JOIN XS_KC; 说明:SELECT语句中只选取一个用来连接表的列时,可以使用自然连接代替内连接。用这种方法,可以用自然左外连接来替换左外连接,自然右外连接替换右外连接。(用的不多,容易出错,因为是让系统自动找相同的条件,找不到时会报错) 注意:外连接只能对两个表进行。 C.交叉连接(指定了CROSS JOIN关键字的连接是交叉连接。) 在不包含连接条件,交叉连接实际上是将两个表进行笛卡儿积运算,结果表是由第一个表的每行与第二个表的每一行拼接后形成的表,因此结果表的行数等于两个表行数之积。 在MySQL中,CROSS JOIN从语法上来说与INNER JOIN等同,两者可以互换。 例: 列出学生所有可能的选课情况。   SELECT 学号, 姓名, 课程号, 课程名      FROM XS CROSS JOIN KC; 另外,STRAIGHT_JOIN连接用法和INNERJOIN连接基本相同。不同的是,STRAIGHT_JOIN后不可以使用USING子句替代ON条件。 例: 使用STRAIGHT_JOIN连接实现例4.22中相同的结果。 SELECT  DISTINCT 课程名, XS_KC.课程号     FROM KC STRAIGHT_JOIN XS_KC    ON  (KC.课程号=XS_KC.课程号); 4.几种连接的举例比较: 要求:查询每个学生的姓名,专业名,课程名,成绩 数据源:xs:姓名,专业名;  kc:课程名 ;   xs_kc  :成绩 连接条件:xs.学号=xs_kc.学号 and  kc.课程号=xs_kc.课程号 (1) 全连接(等值连接)  例1: select 姓名,专业名,课程名,成绩  from xs,kc,xs_kc where xs.学号=xs_kc.学号 and kc.课程号=xs_kc.课程号;  例2: select 姓名,专业名,课程名,成绩 fromxs,kc,xs_kc where xs.学号=xs_kc.学号 and kc.课程号=xs_kc.课程号 and 课程名='计算机基础' and 成绩>=80 order by 成绩 desc; (2) 内连接 例1: select xs.学号,姓名,课程号,成绩 from xs inner join xs_kc on xs.学号=xs_kc.学号; 例2: select 姓名,专业名,课程名,成绩 from xs inner join xs_kc on xs.学号=xs_kc.学号 inner join kc on xs_kc.课程号=kc.课程号 where 课程名='计算机基础' and 成绩>=80; 5.连接多表(如果是三张表) 1).Join连接: from  表1 inner join 表2 on 条件1                 inner join 表3 on 条件2      2).全连接: from  表1,表2,表3 where条件1   and  条件2 3).给表加别名:from  表名 as 别名 注意:如果给表加了别名,以后的使用必须用别名,不能再使用原表名   作者 tianyazaiheruan bitsCN.com

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Implement error handling mechanism for database queries in React Query Implement error handling mechanism for database queries in React Query Sep 28, 2023 pm 02:40 PM

Implementing the error handling mechanism of database queries in ReactQuery ReactQuery is a library for managing and caching data, and it is becoming increasingly popular in the front-end field. In applications, we often need to interact with databases, and database queries may cause various errors. Therefore, implementing an effective error handling mechanism is crucial to ensure application stability and user experience. The first step is to install ReactQuery. Add it to the project using the following command: n

Laravel middleware: Add database querying and performance monitoring to your application Laravel middleware: Add database querying and performance monitoring to your application Jul 28, 2023 pm 02:53 PM

Laravel Middleware: Adding Database Query and Performance Monitoring to Applications Introduction: Data query and performance monitoring are very important when developing web applications. Laravel provides a convenient way to handle these requirements, namely middleware. Middleware is a technology that handles between requests and responses. It can perform some logic before the request reaches the controller or after the response is returned to the user. This article will introduce how to use Laravel middleware to implement database query and performance monitoring. 1. Create the middle

How to solve the problem of database query number overflow in Java development How to solve the problem of database query number overflow in Java development Jun 29, 2023 pm 06:46 PM

How to solve the problem of database query number overflow in Java development. Title: How to solve the database query number overflow problem in Java development. Abstract: With the development of the Internet and the gradual increase in the amount of data, the number of database queries is also increasing. In Java development, due to memory limitations, you may encounter the problem of overflow in the number of database queries. This article will introduce several ways to solve this problem. Text: Optimizing database query statements First, we can solve this problem from the perspective of optimizing database query statements. we can use

PHP database query tips: How to use the mysqli_query function to perform SQL queries PHP database query tips: How to use the mysqli_query function to perform SQL queries Jul 29, 2023 pm 04:42 PM

PHP database query tips: How to use the mysqli_query function to perform SQL queries When developing PHP applications, interacting with the database is a very important part. For query operations, PHP provides some built-in functions to execute SQL statements. This article will focus on how to use the mysqli_query function to help developers better perform database query operations. 1. Introduction to mysqli_query function The mysqli_query function is a built-in function of PHP.

How to query a database and display the results using PHP How to query a database and display the results using PHP May 02, 2024 pm 02:15 PM

Steps to use PHP to query the database and display the results: connect to the database; query the database; display the results, traverse the rows of the query results and output specific column data.

How to implement the statement to view table data in MySQL? How to implement the statement to view table data in MySQL? Nov 08, 2023 pm 01:40 PM

Title: Statements and specific code examples for viewing table data in MySQL MySQL is an open source relational database management system that is widely used in applications of all sizes. In MySQL, viewing table data is a very basic operation. The following will introduce how to implement this operation through specific statements and code examples. First, we will introduce the statements and specific code examples for viewing table data through the MySQL command line tool. Suppose we have a table named "employees", the following is the pass

PHP High Performance: How to Optimize Database Queries PHP High Performance: How to Optimize Database Queries Jun 04, 2023 am 08:40 AM

In the current Internet era, with the explosive growth of data, databases have become the core of a service. The performance and speed of the database directly affect the user experience and usability of the website and its applications. Therefore, how to optimize database queries is an issue that developers need to focus on. In the PHP language, through the optimization of database query statements, the performance of the program can be improved, the burden on the server can be reduced, and the stability of the service can be improved. This article will introduce how to optimize database queries from the following aspects: 1. Using indexes when performing queries

How to perform full-text search in PHP? How to perform full-text search in PHP? May 13, 2023 am 08:00 AM

With the continuous development of Internet technology, the explosive growth of data volume and the widespread application of various text data, full-text retrieval has become a very important technology. Full-text search is a method that can quickly and accurately find text data. It is widely used in application scenarios such as search engines, forums, blogs, and e-commerce websites. How to implement full-text search in PHP programming? 1. What is full-text search? In traditional relational databases, we usually use SQL statements for fuzzy queries. However, when the amount of data is large, this query method will

See all articles