MySQL之进化篇

MySQL之实用篇 MySQL之牛刀小试

子查询是指出现在其他SQL语句内的SELECT子句. 例如: SELECT * FROM t1 WHERE column1 = (SELECT column2 FROM t2) 其中 SELECT * FRIN t1 称为outerQuery SELECT column2 FROM t2 称为subQuery 注意:子查询指嵌套在查询内部,且必须始终出现在圆括号内. 子查询可以包含多个关键字或者条件,如 DISTINCT , GROUP BY, ORDER BY,函数等. 子查询的外层查询可以是SELECT , INSERT, UPDATE,SET 或DO. 子查询可以返回标量,一行,一列或子查询.

###下面是我创建的goods表结构以及添加的一些数据

###子查询 ######查询商品价格的平均值

SELECT AVG(goods_price) FROM goods
复制代码

######查询商品价格的平均值并对值进行四舍五入取两位小数 SELECT ROUND(AVG(goods_price),2) FROM goods

######子查询 查询出价格大于平均值得商品 SELECT goods_id,goods_name,goods_price FROM goods WHERE goods_price > ANY (SELECT goods_price FROM goods WHERE goods_cate = '超级本') ORDER BY goods_price DESC SELECT goods_id,goods_name,goods_price FROM goods WHERE goods_price NOT IN (SELECT goods_price FROM goods WHERE goods_cate = '超级本') ORDER BY goods_price DESC

  • AVG,MAX,MIN、COUNT、SUM为聚合函数(平均值,最大值,最小值,行数,求和)
  • ROUND 是四舍五入 2代表保留两位小数
  • 用ANY,SOME或ALL修饰比较运算符 (ANY,SOME 代表的满足任意一个就可以,ALL代表必须全部满足)
  • [NOT] IN ,[NOT] EXISTS

多表插入

INSERT [INTO] table_name[(column_name,....)] SELECT ...

再创建一个新的表

CREATE TABLE IF NOT EXISTS goods_cates(cate_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, cate_name TEXT NOT NULL)
复制代码

######查询商品的分类 SELECT goods_cate FROM goods GROUP BY goods_cate ######插入所有商品的分类到goods_cate分类的表中 INSERT INTO goods_cates(cate_name) SELECT goods_cate FROM goods GROUP BY goods_cate ###多表更新 ######UPDATE table_references SET column_name1 = {expr1 | DEFAULT} [, column_name2 = {expr2 | DEFAULT}] ...... [WHERE where_condition]

for example:

UPDATE goods INNER JOIN goods_cates ON goods_cate = cate_name SET goods_cate = cate_id
复制代码

CREATE ...SELECT

######创建数据表的同时将查询结果写入到数据表 CREATE TABLE [IF NOT EXISTS] table_name [(create_definition,....)] select_statement for example:

CREATE TABLE goods_brands (brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, brand_name TEXT NOT NULL) SELECT brand_name FROM goods GROUP BY brand_name
复制代码

######再进行刚刚的多表更新 UPDATE goods INNER JOIN goods_brands ON goods.brand_name = goods_brands.brand_name SET goods.brand_name = goods_brands.brand_id ###连接 ######MySQL在SELECT 语句,多表更新,多表删除语句中支持JOIN 操作 #####语法结构 table_reference {[INNER | CROSS] JOIN | {LEFT | RIGHT} [OUTER] JOIN} table_reference ON conditional_expr

A表 + 连接类型{INNER JOIN | LEFT JOIN | RIGHT JOIN} + B表 ON + 连接的条件

#####数据表参照 table_reference

table_name [[AS] alias] | table_subquery [AS] alias
复制代码
  • 数据表可以使用table_name AS alias_name 或 table_name alias_name 赋予别名
  • table_subquery可以作为子查询使用在FROM子句中,这样的子查询必须为其赋予别名 #####连接类型
  • INNER JOIN (内连接) 在MySQL中,JOIN , CROSS JOIN 和 INNER JOIN 是等价的
  • LEFT [OUTER] JOIN (左外连接)
  • RIGHT [OUTER] JOIN (右外连接) ######注意:使用ON关键字来设定连接条件,也可以使用WHERE来代替通常使用ON关键字来设定连接条件,使用WHERE关键字进行结果集记录的过滤 #####内连接

for example:

SELECT goods_id, goods_name, cate_name FROM goods INNER JOIN goods_cates ON goods_cate = cate_id
复制代码

###外链接

  • 如果使用内连接查找的记录在连接数据表中不存在,并且在WHERE子句中尝试一下操作: column_name IS NULL 时,如果column_name被定义为NOT NULL, MySQL将在找到符合连接条件的记录后停止搜索更多的行
  • A LEFT JOIN B join_condition
  • 数据表B的结果集依赖数据表A
  • 数据表A的结果集根据左连接条件依赖所有数据表(B表除外)
  • 左外连接条件决定如何缩减数据表B(在没有指定WHERE条件的情况下)
  • 如果数据表A的魔调记录符合WHERE条件,但是在数据表B不存在连接条件的记录,将会生成一个所有列为空的额外的B行 #####左外连接

for example:

SELECT goods_id, goods_name, cate_name FROM goods LEFT JOIN goods_cates ON goods_cate = cate_id
复制代码

#####右外连接

for example:

SELECT goods_id, goods_name, cate_name FROM goods RIGHT
复制代码

######多表连接 ######我们先把goods表的两个字段名称以及类型修改一下 ALTER TABLE goods CHANGE brand_name brand_id SMALLINT NOT NULL UNSIGNED ALTER TABLE goods CHANGE goods_cate cate_id SMALLINT NOT NULL UNSIGNED

for example :

SELECT goods_id,goods_name, goods_price, cate_name ,brand_name FROM goods AS good 
INNER JOIN goods_cates AS cate ON good.cate_id = cate.cate_id 
INNER JOIN goods_brands AS brand ON good.brand_id = brand.brand_id
复制代码

###无限分类的数据表设计 CREATE TABLE goods_types ( type_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMEN type_name TEXT NOT NULL, parent_id SMALLINT UNSIGNED NOT NULL DEFAULT 0 )

  • INSERT goods_types(type_name,parent_id) VALUES('家用电器',DEFAULT);

  • INSERT goods_types(type_name,parent_id) VALUES('电脑、办公',DEFAULT);

  • INSERT goods_types(type_name,parent_id) VALUES('大家电',1);

  • INSERT goods_types(type_name,parent_id) VALUES('生活电器',1);

  • INSERT goods_types(type_name,parent_id) VALUES('平板电视',3);

  • INSERT goods_types(type_name,parent_id) VALUES('空调',3);

  • INSERT goods_types(type_name,parent_id) VALUES('电风扇',4);

  • INSERT goods_types(type_name,parent_id) VALUES('饮水机',4);

  • INSERT goods_types(type_name,parent_id) VALUES('电脑整机',2);

  • INSERT goods_types(type_name,parent_id) VALUES('电脑配件',2);

  • INSERT goods_types(type_name,parent_id) VALUES('笔记本',9);

  • INSERT goods_types(type_name,parent_id) VALUES('超级本',9);

  • INSERT goods_types(type_name,parent_id) VALUES('游戏本',9);

  • INSERT goods_types(type_name,parent_id) VALUES('CPU',10);

  • INSERT goods_types(type_name,parent_id) VALUES('主机',10);

######查询父类下边的子类 SELECT s.type_id,s.type_name,p.type_name FROM goods_types AS s LEFT JOIN goods_types AS p ON s.parent_id = p.type_id ######查父类下的父类的子类 SELECT p.type_id,p.type_name,s.type_name FROM goods_types AS p LEFT JOIN goods_types AS s ON s.parent_id = p.type_id GROUP BY p.type_name ORDER BY p.type_id

SELECT p.type_id,p.type_name,s.type_name child_count FROM goods_types AS p LEFT JOIN 
goods_types AS s ON s.parent_id = p.type_id
复制代码

###多表删除 DELETE table_name [.] [table_name[.]]..... FROM table_references [WHERE where_condition]

for example:

SELECT goods_id,goods_name FROM goods GROUP BY goods_name HAVING count(goods_name) >= 2;DELETE t1 FROM goods AS t1 LEFT JOIN (SELECT goods_id,goods_name FROM goods GROUP BY goods_name HAVING count(goods_name) >= 2) AS t2 ON t1.goods_name = t2.goods_name WHERE t1.goods_id > t2.goods_id;
复制代码

转载于:https://juejin.im/post/5a31ea556fb9a045204c3b7e

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/278888.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android 9.0新ui,SystemUI分析(Android9.0)

8种机械键盘轴体对比本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?一、SystemUI组成SystemUI是Android的系统界面,包括状态栏statusbar、锁屏keyboard、任务列表recents等等,都继承于SystemUI这个类&#xf…

WMI技术介绍和应用——WMI概述

https://blog.csdn.net/breaksoftware/article/details/8424317转载于:https://www.cnblogs.com/diyunpeng/p/9982885.html

解决App启动时白屏的问题

第一次 03-25 11:02:34.431 6908-6908/com.newenergyjinfu.jytz D/App: before_onCreate: 239 03-25 11:02:34.513 6908-6908/com.newenergyjinfu.jytz D/App: after_initOkGo( initPicasso): 316 03-25 11:02:34.570 6908-6908/com.newenergyjinfu.jytz D/App: after_ J…

chromebook刷机_如何为不支持Chrome操作系统的网站欺骗Chromebook用户代理

chromebook刷机Not all browsers handle websites the same, and if they don’t support your operating system or browser, you could be denied access. Luckily, you can spoof the user agent on Chrome OS to make it look like you use a completely different system.…

什么时候可以升级HarmonyOS,华为鸿蒙OS即将迎来升级 手机版本或仍需时间

原标题:华为鸿蒙OS即将迎来升级 手机版本或仍需时间在2019年的华为开发者大会上,华为消费者业务CEO余承东正式对外发布了HarmonyOS。时隔一年后,华为开发者大会2020即将拉开帷幕。此次大会,HarmonyOS无疑仍会是重头戏之一&#xf…

Shell_mysql命令以及将数据导入Mysql数据库

连接MYSQL数据库 mysql -h${db_ip} -u${db_user} -p${db_pawd} -P${db_port} -D${db_name} -s -e "${sql}" db_ip:主机地址 db_user :数据库用户名 db_pwd:密码 db_port:端口号 db_name:数据库名称 sql&…

cocos android-1,cocos2dx在windows下开发,编译到android上(1)

转自:http://www.2cto.com/kf/201205/130697.html下面我给大家介绍下,用vs2010开发cocos2dx,然后如何使其编译到android上。步骤如下:1、必要条件,你的eclipse能把代码编译到安卓手机或虚拟机上,如果这一步…

中药ppi网络图太杂乱_太杂乱了吗? 这是您的iPhone,iPad,Android或台式机的15张简约壁纸...

中药ppi网络图太杂乱Busy wallpaper images don’t work very well on your iPhone, iPad, or any device where you need to have lots of icons on the screen. Here’s a set of minimalistic wallpaper images that won’t clutter up your desktop. 繁忙的墙纸图像在iPhon…

算法61---两个字符串的最小ASCII删除和【动态规划】

一、题目: 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和。 示例 1: 输入: s1 "sea", s2 "eat" 输出: 231 解释: 在 "sea" 中删除 "s" 并将 "s" 的值(115)加入总和。 在…

android设置时间widget,【Android】时间与日期Widget(DatePicker 与 TimePicker)

public class Activity01 extends Activity{TextViewm_TextView;//声明dataPickerDatePickerm_DatePicker;//声明TimePickerTimePickerm_TimePicker;Button m_dpButton;Button m_tpButton;//java中的Calendar类Calendar c;/** Called when the activity is first created. */Ov…

初学者java学习计划_初学者:计划在Windows 7 Media Center中录制直播电视的时间

初学者java学习计划If you’re a new user to Windows 7 Media Center you know it can act as a DVR and pause or record Live TV. You can set up a schedule for it to record your favorite TV programs as well. 如果您是Windows 7 Media Center的新用户,则知…

双数据源配置

从此抄录&#xff1a;https://blog.csdn.net/ll535299/article/details/78203634 1、先配置两个数据源&#xff0c;附上主要代码&#xff0c;给自己回忆&#xff0c;详解见开头链接 <!-- 配置数据源 --> <bean id"szDS" class"com.alibaba.druid.pool.…

如何在Office 2007中查看关于对话框和版本信息

One of our favorite readers wrote in today asking how to tell if his Word 2007 installation was running Service Pack 1, since he couldn’t find the About dialog, which got me thinking… I bet most people don’t know where it is! 我们最喜欢的一位读者今天写信…

windows全局热键_在Windows中创建快捷方式或热键以清除剪贴板

windows全局热键Have you ever copied something to the clipboard that you don’t want to leave there in case somebody else is going to use your computer? Sure, you can copy something else to the clipboard real quick, but can’t you just make a shortcut or h…

android+notepad教程,Android Sample学习——NotePad

android.view.Menu专场Interface for managing the items in a menu.By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating…

Windows应用程序开发

Windows窗体应用程序开发&#xff1a;WinForm、桌面应用程序&#xff0c;有可执行文件(.exe)即安装包。是一种C/S&#xff08;客户机/服务器&#xff09;架构应用程序 1.Windows窗体应用程序&#xff0c;用可视化的窗体和控件生成丰富界面的&#xff0c;可交互操作的应用程序。…

获取outlook 会议_如何仅在Microsoft Outlook中仅获取您关注的电子邮件的通知

获取outlook 会议Some emails are more important than others. Rather than getting alerts every time an email arrives, configure Microsoft Outlook to only alert you when the important stuff hits your inbox, rather than any old email that can wait until you ch…

jq html 多一个引号,为什么jQuery模板会为某些字符串添加双引号

背景我正在使用jQuery模板,ASP.Net MVC Razor视图和Twitter.问题使用带有一些字符串的jQuery模板会自动导致这些字符串被包含在“细节我创建了一个如下所示的jQuery模板&#xff1a;before ${text.parseUserName().parseHashTag()} after${created_at}${prettyDate(created_at)…

从Windows计算机上完全删除iTunes和其他Apple软件

If you are giving up on iTunes for another music player, uninstalling it completely can be a hassle. Here we show you how to completely remove all traces of it including QuickTime, iTunes Helper, Bonjour…all of it. 如果您在iTunes上放弃了其他音乐播放器&…

html仿微信滑动删除,使用Vue实现移动端左滑删除效果附源码

左滑删除在移动端是很常见的一种操作&#xff0c;常见于删除购物车中的商品&#xff0c;删除收藏夹中文章等等场景。我们只需要手指按住要删除的对象&#xff0c;然后轻轻向左滑动&#xff0c;便会出现删除按钮&#xff0c;然后点击删除按钮即可删除对象。点击下载源码今天我给…