《MySQL必知会》第13章 分组数据

group by 若使用了分组函数,则聚集函数只会对每个分组内的数据进行操作 如果分组中有null值,它们将会被当成一组 group by在where之后,order by之前 使用ROLLUP使用WITH ROLLUP关键字,可以得到每个分组以及每个分组汇总级别(针对每个分组)的值,如下所示: 1 2 3 selectusername,sum(order_cost)fromordersgroupbyusernamewithrollup; 使用having过滤分组 where过滤行 having过滤分组

created: 2023-04-04  |  updated: 2023-04-04  |  阿秀

《MySQL必知会》第14章 使用子查询

子查询 子查询总是从内到外 子查询作为where条件 子查询作为计算字段 子查询中的WHERE子句与前面使用的WHERE子句稍有不同,因为它使用了完全限定列名 相关子查询:涉及外部查询的子查询

created: 2023-04-04  |  updated: 2023-04-04  |  阿秀

《MySQL必知会》第15章 联结表

通过where子句联结 返回笛卡尔积的情况: 1 selectdog_name,cat_namefromdogs,cats; 通过join语句联结

created: 2023-04-04  |  updated: 2023-04-04  |  阿秀

《MySQL必知会》第16章 创建高级联结

起别名 避免引用同一个表,造成指代二义性 外部连接 left outer join

created: 2023-04-04  |  updated: 2023-04-04  |  阿秀

《MySQL必知会》第17章 组合查询

union UNION中的每个查询必须包含相同的列、表达式或聚集函数(不过各个列不需要以相同的次序列出) 使用union时重复的行会被自动取消(若想保留,使用union all) 在使用union时,order by只能出现在最后的select语句后面

created: 2023-04-04  |  updated: 2023-04-04  |  阿秀

《MySQL必知会》第18章 全文本搜索

创建表时启用全文搜索 1 2 3 4 5 6 7 createtableproductnotes(note_idintnotnullauto_increment,prod_idchar(10)notnull,note_texttextnull,primarykey(note_id),fulltext(note_text),)engine=myisam; 新版本的mysql,innodb也支持全文索引了。 快速导入数据时,可以先不设置fulltex,导入完毕后再设置。 进行全文搜索 1 2 3 selectnote_textfromproductnoteswherematch(note_text)against('rabbit') 其中match指定要搜索的列,against指定值 注意是在where中 rank 1 2 3 4 selectnote_text,match(note_text)against('rabbit')asrankfromproductnots; rank是一个浮点数,若没有匹配为0 查询扩展 1 2 3 selectnote_textfromproductnoteswherematch(note_text)against('rabbit'withqueryexpansion); 根据rabbit查出的结果,(例如查出cat)拓展的去查cat的内容 布尔文本搜索 布尔文本搜索提供更多的定制化搜索: 要匹配的词 要排斥的词(若包含要排斥的词则不返回该行) 排列提示(指定优先级) 表达式分组 值得注意的是,即使没有full text索引,也可以使用布尔文本索引,但这是一种非常缓慢的操作。 略

created: 2023-04-04  |  updated: 2023-04-04  |  阿秀

《MySQL必知会》第19章 插入数据

插入完整的行 1 insertintotb_namevalues(val_1,val_2...,val_n); 可以不指定列,但是values里的值顺序要一致,且不能缺少。 省略列: 列设置了默认值 列可以被设置为null 使用insert low_priority into 降低insert的优先级 插入行的一部分 指定列即可 插入多行 1 insertintotb_namevalues(val_11,val_12...),(val_21,val_22...); 插入某些查询结果 insert into user( username, password ) select username, password from backUpUser where age > 18;

created: 2023-04-04  |  updated: 2023-04-04  |  阿秀

《MySQL必知会》第20章 更新和删除数据

更新特定行 1 2 3 updateusersetage=age+1whereage>18; ignore关键字 update的时候,如果一行update失败,则由于事务的特性会导致整个update操作回退, 使用ignore可以忽略本行update的失败 1 2 3 updateignoreusersetage=age+1whereage>18; 更新全部行 删除特定行 1 deletefromuserwhereusername='mao'; 删除全部行 如果想删除表中所有的行,不要使用delete,可以使用truncate table语句(原理是删除原来的表,并新建同名表)

created: 2023-04-04  |  updated: 2023-04-04  |  阿秀