###########################
#第十七章 组合查询
###########################
###创建组合查询
##union(重复的行自动取消)
select vend_id,prod_id,prod_price from products where prod_price<=5
union
select vend_id,prod_id,prod_price from products where vend_id in (1001,1002);
#等价于
select vend_id,prod_id,prod_price from products
where prod_price<=5 or vend_id in (1001,1002);
#包含或取消重复的行 (包含重复的行)union all
select vend_id,prod_id,prod_price from products where prod_price<=5
union all
select vend_id,prod_id,prod_price from products where vend_id in (1001,1002);
#对组合查询结果排序
select vend_id,prod_id,prod_price from products where prod_price<=5
union
select vend_id,prod_id,prod_price from products where vend_id in (1001,1002)
order by vend_id,prod_price;
#看似只对最后一个语句排序 其实是对组合结果排序
######################
#第十八章 全文本搜索
######################
#1,引擎myisam
#2,创建时 FULLTEXT(note_text)
CREATE TABLE productnotes
(
note_id int NOT NULL AUTO_INCREMENT,
prod_id char(10) NOT NULL,
note_date datetime NOT NULL,
note_text text NULL ,
PRIMARY KEY(note_id),
FULLTEXT(note_text)
) ENGINE=MyISAM;
#进行全文本搜索
#match()指定被索引的列 against()指定要使用的表达式
select note_text from productnotes
where match(note_text) against('rabbit');
#等价于
select note_text from productnotes
where note_text like '%rabbit%';
###全文本搜索对结果排序,较高等级的先返回,而Like却没有
##看看等级是怎么展示的
select note_text,
match(note_text) against('rabbit') as rank
from productnotes;
#使用查询扩展 比较下面的不同
select note_text from productnotes
where match(note_text) against ('anvils');
select note_text from productnotes
where match(note_text) against ('anvils' with query expansion);
#布尔文本搜索
#布尔方式不同于迄今使用的全文本搜索,即使没有定义fulltext 也可以使用
select note_text from productnotes
where match(note_text) against('heavy' in boolean mode);
select note_text from productnotes
where match(note_text) against('heavy -rope*' in boolean mode);#匹配含有heavy但是不含rope
## + 词必须存在 - 词必须不存在
## > 包含,增加等级 < 包含 ,减少等级
## ()把词组成子表达式 ~ 取消一个词的排序值
## * 词尾的通配符 ''''定义一个短语(看做整体匹配)
select note_text from productnotes
where match(note_text) against('+rabbit +bait' in boolean mode);#包含两个词rabbit bait的所有行
select note_text from productnotes
where match(note_text) against('rabbit bait' in boolean mode);#这两个词至少包含一个
select note_text from productnotes
where match(note_text) against('''rabbit bait''' in boolean mode);#两个词要连续出现
select note_text from productnotes
where match(note_text) against('>rabbit <bait' in boolean mode);#至少一个 增加前者等级 降低后者等级
select note_text from productnotes
where match(note_text) against('+rabbit +(<bait)' in boolean mode);#两个都要匹配 减少后者等级