sql中级语句
创建联结
select n_title,n_content,t_name,t_memo from nrc_news,nrc_type where nrc_news.t_id=nrc_type.t_id;
select n_title,n_content,t_name,t_memo from nrc_news,nrc_type ;
笛卡尔积:
由没有联结条件的表关系返回的结果为笛卡尔积。检索出的行的数目将是第一个表中的行数乘以第二个表中的行数。
提示:返回笛卡尔积的联结也叫叉联结。
内联结:
select n_title,n_content,t_name,t_memo from nrc_news inner join nrc_type on nrc_news.t_id=nrc_type.t_id;
上述语句用法与第一条一样。
多表联结:
select r_content,n_title,n_content,t_name,t_memo from nrc_review,nrc_news,nrc_type where nrc_review.n_id=nrc_news.n_id and nrc_news.t_id=nrc_type.t_id;
select r_content,n_title,n_content,t_name,t_memo from nrc_review,nrc_news,nrc_type where nrc_review.n_id=nrc_news.n_id and nrc_news.t_id=nrc_type.t_id and t_memo='小火龙';
创建高级联结:
别名:
select r_content,n_title,n_content,t_name,t_memo from nrc_review as r,nrc_news as n,nrc_type as t where r.n_id=n.n_id and n.t_id=t.t_id;
自联结:
select n_id,n_title,n_content,t_id from nrc_news where t_id=(select t_id from nrc_news where n_title='梦幻');
外联结:
select nrc_news.n_title,nrc_news.n_content,nrc_type.t_name,nrc_type.t_memo from nrc_news left outer join nrc_type on nrc_news.t_id=nrc_type.t_id;
select nrc_news.n_title,nrc_news.n_content,nrc_type.t_name,nrc_type.t_memo from nrc_news right outer join nrc_type on nrc_news.t_id=nrc_type.t_id;
提示:
外联结的类型:
外联结两种类型:左外联结和右外联结,它们唯一的区别是所关联的表的顺序。换句话说,调整FROM或WHERE子句中表的顺序,左外联结可以转换为右外联结。
还存在另一种外联结,就是全外联结,它检索两个表中的所有行并关联那些可以关联的行。与左右外联结包含一个表的不关联的行不同,全外联结包含两个表的不关联的行。
带有聚集函数的联结
select t.t_id,COUNT(n.t_id)as tchoose from nrc_type as t Inner join nrc_news as n on t.t_id=n.t_id group by t.t_id;
组合查询:
select * from nrc_type where t_id in(1,2,3)
union
select * from nrc_type where t_name ='公告';
union相当于或
上述语句等于
select * from nrc_type where t_id in(1,2,3) or t_name ='公告';
sql中级语句的更多相关文章
- [转]MySQL 最基本的SQL语法/语句
MySQL 最基本的SQL语法/语句,使用mysql的朋友可以参考下. DDL-数据定义语言(Create,Alter,Drop,DECLARE) DML-数据操纵语言(Select,Delete ...
- SQL入门语句之ORDER BY 和GROUP BY
一.SQL入门语句之ORDER BY ORDER BY 是用来基于一个或多个列按升序或降序顺序排列数据 1.从数据库表获取全部数据按字段A的升序排列 select *from table_name o ...
- SQL入门语句之LIKE、GLOB和LIMIT
一.SQL入门语句之LIKE LIKE用来匹配通配符指定模式的文本值.如果搜索表达式与模式表达式匹配,LIKE 运算符将返回真(true),也就是 1.这里有两个通配符与 LIKE 运算符一起使用,百 ...
- SQL入门语句之SELECT和WHERE
一.SQL入门语句之SELECT SELECT语句用于从数据库表中获取数据,结果表的形式返回数据.这些结果表也被称为结果集 1.从数据库表中取部分字段 select 字段A,字段B from tabl ...
- SQL入门语句之INSERT、UPDATE和DELETE
一.SQL入门语句之INSERT insert语句的功能是向数据库的某个表中插入一个新的数据行 1.根据对应的字段插入相对应的值 insert into table_name(字段A, 字段B, 字段 ...
- 快速将一个表的数据生成SQL插入语句
将一个表中的数据生成SQL插入语句,方便系统快速初始化,在数据库中执行创建以下过程就可以了. ) Drop Procedure GenerateData go CREATE PROCEDURE Gen ...
- sql查询语句如何解析成分页查询?
我们公司主要mysql存储数据,因此也封装了比较好用mysql通用方法,然后,我们做大量接口,在处理分页查询接口,没有很好分查询方法.sql查询 语句如何解析成“分页查询”和“总统计”两条语句.可能, ...
- 规则引擎集成接口(四)SQL执行语句
SQL执行语句 右键点击数据库连接文件“hr”—“添加SQL执行语句”,如下图: 弹出窗体,如下图: 将显示名称改为“部门名称”,返回至类型设置为“string”,在编写sql语句,如下图: 点击确定 ...
- SQL SELECT 语句
本章讲解 SELECT 和 SELECT * 语句. SQL SELECT 语句 SELECT 语句用于从表中选取数据. 结果被存储在一个结果表中(称为结果集). SQL SELECT 语法 SE ...
随机推荐
- nginx反向代理tomcat 时,出现https redirect后变成http的问题解决方法
需要修改两个配置 1.nginx配置 location / { proxy_pass http://test-server; proxy_set_header Host $host; proxy_se ...
- 和PHP相关的Linux命令
Linux服务器上怎么找到php.ini php -ini #输出一堆信息,里面有loaded configuration file => /etc/php/7.0/cli/php.ini就是了 ...
- RabbitMQ的下载与安装
RabbitMQ的安装注意事项: 1. 系统的管理员账户不能是中文(win8) 2. 计算机名不能是中文(win8) 3. 推荐:使用默认的安装目录 4. 使用的计算机用户必须是管理员 如果安装不成功 ...
- javascript数组的增删改和查询
数组的增删改操作 对数组的增删改操作进行总结,下面(一,二,三)是对数组的增加,修改,删除操作都会改变原来的数组. (一)增加 向末尾增加 push() 返回新增后的数组长度 arr[arr.leng ...
- NFFM的原理与代码
本篇深入分析郭大nffm的代码 TensorFlow计算图 计算图的构建 ones = tf.ones_like(emb_inp_v2) mask_a = tf.matrix_band_part(on ...
- Difference between C# compiler version and language version
Difference between C# compiler version and language version As nobody gives a good enough answer ...
- yield and send的使用详细解释
https://blog.csdn.net/mieleizhi0522/article/details/82142856 虽然并不完全正确,但是能在使用中帮我们拨开迷雾 再结合另外一篇文章理解了htt ...
- ResourceUtils 创建资源目录工具类
package com.jcf.utilsdemo; import android.content.Context; import android.content.res.Resources; pub ...
- 处理输入为非对角阵的Clustering by fast search and find of density peak代码
Clustering by fast search and find of density peak. Alex Rodriguez, Alessandro Laio 是发表在Science上的一篇很 ...
- Ubuntu16.04格式化U盘
root@ubuntu:~# fdisk -l root@ubuntu:~# fdisk /dev/sdb 格式化U盘: root@ubuntu:~# fdisk -l sudo mkfs.ntfs ...