创建联结
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中级语句的更多相关文章

  1. [转]MySQL 最基本的SQL语法/语句

    MySQL 最基本的SQL语法/语句,使用mysql的朋友可以参考下.   DDL-数据定义语言(Create,Alter,Drop,DECLARE) DML-数据操纵语言(Select,Delete ...

  2. SQL入门语句之ORDER BY 和GROUP BY

    一.SQL入门语句之ORDER BY ORDER BY 是用来基于一个或多个列按升序或降序顺序排列数据 1.从数据库表获取全部数据按字段A的升序排列 select *from table_name o ...

  3. SQL入门语句之LIKE、GLOB和LIMIT

    一.SQL入门语句之LIKE LIKE用来匹配通配符指定模式的文本值.如果搜索表达式与模式表达式匹配,LIKE 运算符将返回真(true),也就是 1.这里有两个通配符与 LIKE 运算符一起使用,百 ...

  4. SQL入门语句之SELECT和WHERE

    一.SQL入门语句之SELECT SELECT语句用于从数据库表中获取数据,结果表的形式返回数据.这些结果表也被称为结果集 1.从数据库表中取部分字段 select 字段A,字段B from tabl ...

  5. SQL入门语句之INSERT、UPDATE和DELETE

    一.SQL入门语句之INSERT insert语句的功能是向数据库的某个表中插入一个新的数据行 1.根据对应的字段插入相对应的值 insert into table_name(字段A, 字段B, 字段 ...

  6. 快速将一个表的数据生成SQL插入语句

    将一个表中的数据生成SQL插入语句,方便系统快速初始化,在数据库中执行创建以下过程就可以了. ) Drop Procedure GenerateData go CREATE PROCEDURE Gen ...

  7. sql查询语句如何解析成分页查询?

    我们公司主要mysql存储数据,因此也封装了比较好用mysql通用方法,然后,我们做大量接口,在处理分页查询接口,没有很好分查询方法.sql查询 语句如何解析成“分页查询”和“总统计”两条语句.可能, ...

  8. 规则引擎集成接口(四)SQL执行语句

    SQL执行语句 右键点击数据库连接文件“hr”—“添加SQL执行语句”,如下图: 弹出窗体,如下图: 将显示名称改为“部门名称”,返回至类型设置为“string”,在编写sql语句,如下图: 点击确定 ...

  9. SQL SELECT 语句

      本章讲解 SELECT 和 SELECT * 语句. SQL SELECT 语句 SELECT 语句用于从表中选取数据. 结果被存储在一个结果表中(称为结果集). SQL SELECT 语法 SE ...

随机推荐

  1. 爬当当网上python书籍的图片

    1.分析网页代码,获取图片下载连接:http://img3m4.ddimg.cn/20/11/23473514-1_b_5.jpg 2. python实现代码 import os import re ...

  2. [Luogu] 聪明的质监员

    https://www.luogu.org/problemnew/show/P1314 满足单调性 所以,二分W,进行检验 #include <iostream> #include < ...

  3. 小程序弹框wx.showModal、wx.showActionSheet、wx.showToast

    wx.showModal wx.showModal({ title: '删除图片', content: '确定要删除该图片?', showCancel: true,//是否显示取消按钮 cancelT ...

  4. Jquery 2.0+版本不支持IE8,如何解决?

    用了JQuery2.0+以后,在IE8下会报错,下面是我的方法. 先看代码: <!--[if !IE]> -->        <script src="/Scrip ...

  5. Codeforces 955C Sad powers(数论)

    Codeforces 955C Sad powers 题意 q组询问,每次询问给定L,R,求[L,R]区间内有多少个数可以写成ap的形式,其中a>0,p>1,1 ≤ L ≤ R ≤ 1e1 ...

  6. @ConfigurationProperties实现配置注入到实体类

    spring boot 使用@ConfigurationProperties 有时候有这样子的情景,我们想把配置文件的信息,读取并自动封装成实体类,这样子,我们在代码里面使用就轻松方便多了,这时候,我 ...

  7. Java图片裁剪

    public static void main(String[] args) throws IOException { String path = "C:/Users/yang/Deskto ...

  8. async for 在爬虫中的使用例子

    import asyncio import re import typing from concurrent.futures import Executor, ThreadPoolExecutor f ...

  9. nginx部署前端项目

    1.在阿里云服务器上安装nginx,推荐使用yum安装 yum install -y nginx // 命令安装 nginx 服务器 2.配置nginx 安装完成后,进入 nginx 配置文件目录 一 ...

  10. Amdahl定律和可伸缩性

    性能的思考 提升性能意味着可以用更少的资源做更多的事情.但是提升性能会带来额外的复杂度,这会增加线程的安全性和活跃性上的风险. 我们渴望提升性能,但是还是要以安全为首要的.首先要保证程序能够安全正常的 ...