.Do not display duplicate records in query results
Do not display duplicate records when querying mainly applies the DISTINCT keyword, which is used to delete duplicate records.
When implementing query operations, if the query's select list contains the primary key of a table, then the records in each query will be unique (because the primary key has a different value in each record); if the primary key does not Included in the query results, duplicate records may appear. Use the DISTINCT keyword to delete duplicate records. The syntax of
DISTINCT is as follows:
SELECT DISTINCT select_list;
Note: The DISTINCT keyword does not refer to a certain row, but refers to all columns output by SELECT without duplication. This is important because it prevents the same rows from appearing in the output of a query.
For example:
select distinct name,price,date,address,quality from tb;
2. Use NOT to query records that do not meet the conditions
Use the condition formed by combining NOT with the predicate to query. The expressions formed by combining
NOT with predicates are [NOT] BETWEEN, IS [NOT] NULL and [NOT] IN respectively.
(1)[NOT] BETWEEN
This condition specifies the inclusive range of values, using AND to separate the start value and the end value.
The syntax is as follows:
test_expression [NOT] BETWEEN begin_expression AND end_expression
The result type is boolean and the return value is: If the value of test_expression is less than or equal to the value of begin_expression or greater than or equal to the value of end_expression, then NOT BETWEEN returns true.
Note: To specify an exclusion range, you can also use the greater than (>) and less than (
(2)IS [NOT] NULL
Queries for null or non-null values according to the keyword specified. If any operand is null, the expression evaluates to null.
(3) [NOT] IN
Specifies the expression to be queried based on whether the keyword used is included in the list or excluded from the list. Query expressions can use permutations or column names, and lists can be a set of permutations or subqueries (more often). If the list is a set of constants, it should be placed within a pair of parentheses.
The syntax is as follows:
test_expression [NOT] in( subquery expression[,...n] )
Parameter description:
①test_expression: SQL expression
②subquery: A subquery containing a certain column result set, which must have the same data type as test_expression.
③expression[,...n]: A list of expressions used to test whether they match. All expressions must be of the same data type as test_expression j.
For example:
select * from tb where selldate not between '2016-10-30' and '2016-12-12';
3. Use the subquery as an expression
Apply the subquery in the SELECT clause, and the query structure can appear in the form of an expression. There are some control rules when applying subqueries. Understanding these rules will help you better master the application of subqueries.
①The inner query SELECT list or IN introduced by the comparison operator contains only one expression or column name. Columns named in the WHERE clause of the outer statement must be join-compatible with columns named in the query's SELECT list.
②Subqueries introduced by immutable comparison operators (comparison operators not followed by the keywords ANY and ALL) cannot include a GROUP BY clause or a HAVING clause unless the group or individual values are predetermined.
③The SELECT list introduced by EXISTS generally consists of asterisks (*), without specifying specific column names, and you can also nest subqueries to limit rows in the WHERE clause.
④Subqueries cannot process their results internally, that is, subqueries cannot include an ORDER BY clause. The optional DISTINCT keyword is useful for sorting subquery results, since some systems eliminate duplicate records by sorting the results first.
For example: display the total scores of all students and the difference between the students’ total scores and the school’s average score.
select stuId , stuName, (Math+Language+English) Total , round((select avg(Math+Language+English) from tb),0) Averages, round(((Math+Language+English)-(select avg(Math+Language+English) from tb)),0) Average from tb;
4. Use subqueries as derived tables
In practical applications, subqueries are often used as derived tables, that is, the result set of the query is used as a table.
Subquery is an additional method for handling multi-table operations. The syntax structure is as follows:
(SELECT [ALL|DISTINCT]<select item list> From <table list> [WHERE <search condition>] [GROUP BY<group item list> [HAVING <group by search condition>]] )
For example:
Group the sales orders according to the product name statistics and query the products with sales quantity greater than 14 (use the group statistics as a derived table)
select * from (select proname ,COUNT(*) as sl from td GROUP BY proname) WHERE (sl > 14) ;
Perform the top 100 sales quantity in the product sales table Group statistics (use filtered data as a derived table)
select sl,count(*) from ( select * from tb ORDER BY zdbh LIMIT 0,100) GROUP BY sl;
Statistics of the amount owed by unsettled customers in the customer relationship table (use filtered data as a derived table)
select name,sum(xsje) from (select * from tb where NOT pay) GROUP BY name;
Query all warrior training information and query the third shooting score greater than 8 Ring warrior information (use the results of one query as the table operated by another query)
select T.soldId, T.soldName, T.FrirstGun, T.SecondGun, T.ArtideGun from (select * from tb where ArtideGun>8) as T;
Note: The derived table must be aliased.
5. Relate data through subqueries
Use EXISTS predicate to introduce subqueries. In some cases, as long as the subquery returns a true or false value, only whether the predicate condition is met is considered, and the data content itself is not important. At this point you can use the EXISTS predicate to define a subquery. The EXISTS predicate is true if the subquery returns one or more rows, false otherwise. For the EXISTS predicate to work, the query conditions should be established in the subquery to match the values in the two tables joined by the subquery.
The syntax is as follows:
EXISTS subquery
Parameter description:
subquery:一个受限的 SQL 语句(不允许有 COMPUTE 子句和 INTO 关键字) 。
例如:获取英语成绩大于90分的学生信息
select name,college,address from tb_Stu where exists (select name from tb_grades M where M.name=I.name and English>90) ;
备注:EXISTS 谓词子查询中的 SELECT 子句中可使用任何列名,也可以使用任何多个列。这种谓词值只注重是否返回行,而不注重行的内容,用户可以指定列名或者只使用一个“*”。
6.实现笛卡尔乘积查询
笛卡尔乘积查询实现了两张表之间的交叉连接,在查询语句中没有 WHERE 查询条件,返回到结果集中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合条件的数据行数。
笛卡尔乘积的关键字是 CROSS JOIN 。例如,用户信息表中有2条数据,职工信息表中有4条数据,当这两张表应用笛卡尔乘积进行查询时,查询的结果就是2×4=8条。
例如:
select EmpId,EmpName,Depatment,JobTitle,Wages from tb_employees a cross join tb_position b;
备注:在进行多表查询时需要主注意,由于多表可能会出现相同的字段,因此在指定查询字段时,最好为重复的字段起别名,以方便区分。
7.使用 UNION 并运算
UINON 指的是并运算,即从两个或多个类似的结果集中选择行,并将其组合在一起形成一个单独的结果集。
UINON 运算符主要用于将两个或更多查询结果组合为单个结果集,该结果集包含联合查询中所有查询的全部行。在使用 UNION 运算符时应遵循以下准则:
①在使用 UNION 运算符组合的语句中,所有选择列表的表达式数目必须相同(列名、算术表达式、聚集函数等)。
②在使用 UNION 运算符组合的结果集中的相应列或个别查询中使用的任意列的子集必须具有相同的数据类型,并且两者数据类型之间必须存在可能的隐性转换或提供了显式转换。
③利用 UNION 运算符组合的各语句中对应的结果集列出现的顺序必须相同,因为 UNION 运算符是按照各个查询给定的顺序逐个比较各列。
④ UNION 运算符组合不同的数据类型时,这些数据类型将使用数据类型优先级的规则进行转换。
⑤通过 UNION 运算符生产的表中列名来自 UNION 语句中的第一个单独的查询。若要用新名称引用结果集中的某列,必须按第一个 SELECT 语句中的方式引用该列。
例如:
select filenumuber,name,juior,address from tb union select filenumuber,name,senior,address from tk;
8.内外连接查询
1)内联接(典型的联接运算,使用像 = 或 之类的比较运算符)。包括相等联接和自然联接。
内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行。例如,检索 students和courses表中学生标识号相同的所有行。
内连接可以分为等值连接、自然连接和不等值连接。
等值连接使用等号运算符比较被连接列的值,在查询结果中将列出连接表中的所有列,包括重复列。等值连接返回所有连接表中具有匹配值的行。
等值连接查询的语法如下:
select fildList from table1 inner join table2 on table1.column = table2.column;
参数说明:
fildList:要查询的字段列表。
2)外联接。外联接可以是左向外联接、右向外联接或完整外部联接。
在 FROM子句中指定外联接时,可以由下列几组关键字中的一组指定:
1)LEFT JOIN或LEFT OUTER JOIN
左向外联接的结果集包括 LEFT OUTER子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。
2)RIGHT JOIN 或 RIGHT OUTER JOIN
右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。例如 ,表 A 右外连接表 B,结果为公共部分 C 加表 B 的结果集。如果表 A 中没有与表 B 匹配的项,就是用 NULL 进行连接。
3)FULL JOIN 或 FULL OUTER JOIN
完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。
3)交叉联接
交叉联接返回左表中的所有行,左表中的每一行与右表中的所有行组合。交叉联接也称作笛卡尔积。
FROM 子句中的表或视图可通过内联接或完整外部联接按任意顺序指定;但是,用左或右向外联接指定表或视图时,表或视图的顺序很重要。有关使用左或右向外联接排列表的更多信息,请参见使用外联接。
例子:
-------------------------------------------------
a表 id name b表 id job parent_id 1 张3 1 23 1 2 李四 2 34 2 3 王武 3 34 4 a.id同parent_id 存在关系
--------------------------------------------------
1) 内连接
select a.*,b.* from a inner join b on a.id=b.parent_id 结果是 : 1 张3 1 23 1 2 李四 2 34 2
-------------------------------------------------
2)左连接
select a.*,b.* from a left join b on a.id=b.parent_id 结果是 1 张3 1 23 1 2 李四 2 34 2 3 王武 null
-------------------------------------------------
3) 右连接
select a.*,b.* from a right join b on a.id=b.parent_id 结果是 1 张3 1 23 1 2 李四 2 34 2 null 3 34 4
-------------------------------------------------
4) 完全连接
select a.*,b.* from a full join b on a.id=b.parent_id 结果是 张3 1 23 1 李四 2 34 2 null 3 34 4 王武 nul
-------------------------------------------------
备注:内连接与外连接区别?
内连接只返回两张表相匹配的数据;而外连接是对内连接的扩展,可以使查询更具完整性,不会丢失数据。下面举例说明两者区别。
假设有两张表,分别为表A 与 表B,两张表公共部分为 C 。
内连接的连接结果是两个表都存在记录,可以说 A 内连 B 得到的是 C。
表 A 左外连接B,那么A不受影响,查询结果为公共部分C 加表A的记录集。
表A右外连接B,那么B不受影响,查询结果为公共部分C加表B的记录集。
全外连接表示两张表都不加限制。

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment