Home Database Mysql Tutorial MySQL数据检索+查询+全文本搜索_MySQL

MySQL数据检索+查询+全文本搜索_MySQL

Jun 08, 2016 am 08:50 AM
mysql select union

【0】README 0.1)本文部分文字描述转自“MySQL 必知必会”,旨在review“MySQL数据检索+查询+全文本搜索” 的基础知识;
【1】使用子查询 1)查询定义:任何sql 语句都是查询。但此术语一般指 select语句;SQL 还允许创建子查询,即嵌套在其他查询中的查询; 2)利用子查询进行过滤(where子句,in子句) \

2.1)可以把一条select语句返回的结果用于另一条select语句的where子句;
3)作为计算字段使用子查询 3.1)使用子查询的另一种方式是创建计算字段:如需要显式供应商的订单总数和供应商的id和name; \

Attention)显然, select id,name,(select count(*) from product p where p.vendor=v.id group by vendor) as my_count from vendor v;与其他的SQL不同,因为 where p.vendor=v.id group by vendor) as my_count from vendor v 子句 使用了完全限定名比较 内部表和外部表的id是否相等;

【2】联结表 1)主键+外键: 1.1)主键:能够唯一标识每一行数据; 1.2)外键:外键为某个表中的一列,它包含另一个表的主键值,定义了两个表之间的关系; 2)创建联结: \
3)笛卡尔积:由没有联结条件的表关系返回的结果为笛卡尔积。检索出的行的数目是第一个表中的行数乘以第二个表中的行数; \
Attention)不要忘记where子句:应该保证所有联结都有where 子句,否则MySQL 将返回比想要的数据多得多的数据;
4)内部联结(=等值联结) 4.1)定义:目前为止使用的联结称为等值联结,它基于两个表之间的相等测试。这种联结也称为内部联结;
select v.id,v.name from product p, vendor v where p.vendor = v.id;
select v.id,v.name from product p inner join vendor v on p.vendor = v.id;
Copy after login
\
5)联结多个表 \
5.1)我们需要找出购买wang供应商的生产商品的客户id;
select customer_id from t_order o,product p,vendor v where o.prod_id=p.id and p.vendor = v.id and v.name='wang';
Copy after login

\

【3】创建高级联结 for spec info,please visit MySQL的自然联结+外部联结(左外连接,右外连接)+内部联结
Copy after login

【4】组合查询 1)MySQL允许执行多个查询(多条select),并将结果作为单个查询结果集返回。这些组合查询通常称为并(union)或复合查询; 2)有两种case,需要使用组合查询; case1)在单个查询中从不同的表返回类似结构的数据; case2)对单个表执行多个查询,按单个查询返回数据; 3)创建组合查询:可用union 操作符来组合数条SQL 查询;利用union, 可给出多条select 语句,将它们的结果组成成单个结果集; 4)使用union \
5)使用union规则(rules) r1)union必须由两条或两条以上的select 语句组成,语句之间用关键字 union分割; r2)union 中的每个查询必须包含系统的列,表达式或聚集函数(不过各个列不需要以相同的次序给出); r3)列数量类型必须兼容:类型不必完全相同,但必须是 DBMS可以隐含地转换的类型; 6)包含或取消重复的行 6.1)problem+solution: problem)第一条select语句返回4行,第二条select语句返回3行,而在用union组合两条select语句后,只返回了6行而不是7行;原因是 union从查询结果集中自动去除了重复的行(在使用union时,重复的行被自动取消); solution)如果想要返回所有匹配行,可以使用 union all 而不是 union; \ \ 7)对组合查询结果排序:在使用union 组合查询时,只能使用一条 order by 语句,它必须出现在最后一条select 语句之后。 \
 Attention)使用union时,也可以使用不同的表进行组合;
【5】全文本搜索 1)只有 myisam 引擎支持全文本搜索; 2)为了进行全文本搜索,必须索引被搜索的列,而且要随着数据的改变不断地重新索引; 2.1)启用全文本搜索支持:一般在创建表时启用全文本搜索;create table 语句接收fulltext 子句,它给出被索引列的一个逗号分隔的列表; 看个荔枝)通过create演示fulltext子句的使用: \
 对以上代码的分析(Analysis):
A1)MySQL根据子句fulltext(note_text)的指示对它进行索引。这里的fulltext索引单个列,如果需要也可以索引多个列; A2)在定义之后,MySQL自动维护该索引。在增加、更新或删除行时,索引随之自动更新; Attention)不要在导入数据时使用fulltext: 更新索引要花时间,虽然不是很多,但毕竟要花时间。如果正在导入数据到一个新表,此时不应该启用fulltext索引。应该首先导入所有数据,然后再修改表,定义 fulltext,这样有助于更快地导入数据;

2.2)进行全文本搜索:使用match()函数和against()函数质心全文本搜索,match()函数指定被搜索的列,而against()指定要使用的搜索表达式; Attention)以下荔枝转自:http://doc.mysql.cn/mysql5/refman-5.1-zh.html-chapter/functions.html#fulltext-search \
2.3)全文搜索带查询扩展

全文搜索支持查询扩展功能 (特别是其多变的“盲查询扩展功能” )。若搜索短语的长度过短, 那么用户则需要依靠全文搜索引擎通常缺乏的内隐知识进行查询。这时,查询扩展功能通常很有用。例如, 某位搜索 “database” 一词的用户,可能认为“MySQL”、“Oracle”、“DB2” and “RDBMS”均为符合 “databases”的项,因此都应被返回。这既为内隐知识。

在下列搜索短语后添加WITH QUERY EXPANSION,激活盲查询扩展功能(即通常所说的自动相关性反馈)。它将执行两次搜索,其中第二次搜索的搜索短语是同第一次搜索时找到的少数顶层文件连接的原始搜索短语。这样,假如这些文件中的一个 含有单词 “databases” 以及单词 “MySQL”, 则第二次搜索会寻找含有单词“MySQL” 的文件,即使这些文件不包含单词 “database”。下面的例子显示了这个不同之处:

2.4)布尔全文索引:利用IN BOOLEAN MODE修改程序, MySQL 也可以执行布尔全文搜索,提供关于如下内容的细节(Details) D1)要匹配的词; D2)要排斥的词; D3)排列提示(某些词比其他词更重要,更重要的词等级更高); D4)表达式分组; \
\ 关于全文本搜索的使用说明(specification)s1)在索引全文本数据时,短词被忽略且从索引中排除。短词定义为那些具有3个或3个以下字符的词(如果需要,这个数字3可以修改); s2)MySQL 带有一个内建的非用词列表,这些词在索引全文本数据时总是被忽略。如果需要,可以覆盖这个列表; s3)许多词出现的频率很高,搜索它们没有用处,因此,MySQL规定了一条50%规则,如果一个词出现在 50% 以上的行中, 则将它作为一个非用词忽略,50%规则不用于 in boolean mode 中; s4)如果表中的行数少于3行,则全文本搜索不返回结果(因为每个次或者不出现,或者至少出现在50%的行中); s5)忽略词中的单引号,如,don't == dont; s6)不具有词分隔符的语言(包括日文和中文)不能恰当地返回全文本搜索结果; s7)仅在 MyISAM数据库引擎中支持全文本搜索;
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

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

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles