#SELECT 列名1, 列名2, from 表明

#SELECT id,title,content,type from news 效率相对较高
#SELECT * from news *代表所有字段,效率相对较低
#SELECT * from news where id = 1 查询出id=1
#SELECT * from news where type = '国际军事类' int类不需要加单引号,varchar需要单引号

#插入语句

#INSERT info 表名(列名1,列名2)
#INSERT into news(title,content, type) values(
#'年度最惊悚的小说','和凤姐同居的日子','国内八卦')

#更新语句
#update 表名 set 列名1=列1值,列名2=列2值 where 判断条件
#UPDATE news set title = '本月最恐怖',content='我和芙蓉同居的日子' where id = 4

#删除语句
#delete from 表名 where 条件
#delete from news where id = 3

外键查询

外键查询

select news.id, news.title,news.content,

newstype.name from news , newstype where
news.type = newstype.id

外键查询简单写法

select n.id,title,content, t.name from news n, newstype t where
n.type = t.id

改变列名

select n.id,title as 标题,content, t.name from news n, newstype t where
n.type = t.id

#内连接写法1
#select * from prisoner, prtype where type = prtype.id

#内连接写法2
#select * from prisoner inner join prtype on
#type = prtype.id

连锁查询

#外连接
select * from prisoner right join prtype on type = prtype.id

#左连接和内连接显示结果一样
select * from prisoner left join prtype on
type = prtype.id

#全连接
select * from prisoner right join prtype on
type = prtype.id
union
select * from prisoner left join prtype on
type = prtype.id

查询出名字以郭开头犯人的名字(like语句)

查询出名字中含有美的犯人 两个百分号 %%

select * from prisoner where name like '%美%'

以编号排序

select * from prisoner order by id

以编号倒序

select * from prisoner order by id desc

找出最小的编号(min语句)

select min(id) from prisoner

找出最大的编号(max语句)

select max(id) from prisoner

查询出所有犯人编号之和,平均数(sum和avg语句)

select sum(id) from prisoner

select avg(id) from prisoner

查询出犯有吸毒罪的人员个数。(count语句)

SELECT count(prisoner.id) from prisoner ,prtype where type = prtype.id and prtype.name = '吸毒类'

查询出所有犯罪类型的犯罪个数(count+group by 语句)

SELECT count(prisoner.id) as 数量,prtype.name from prisoner ,prtype where type = prtype.id
group by prtype.name

查询出前5个犯人的信息(top语句 mysql要用limit)

select * from prisoner limit 0,5

查询出前2个犯人的信息倒叙(top语句 mysql要用limit)

select * from prisoner order by id desc limit 0,2

SQL语句的一些基本使用以及一些技巧的更多相关文章

  1. mysql学习之 sql语句的技巧及优化

    一.sql中使用正则表达式 select name,email from user where email Regexp "@163[.,]com$"; sql语句中使用Regex ...

  2. 一条Sql语句分组排序并且限制显示的数据条数

    如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...

  3. LINQ to SQL语句(7)之Exists/In/Any/All/Contains

    适用场景:用于判断集合中元素,进一步缩小范围. Any 说明:用于判断集合中是否有元素满足某一条件:不延迟.(若条件为空,则集合只要不为空就返回True,否则为False).有2种形式,分别为简单形式 ...

  4. Oracle ------ SQLDeveloper中SQL语句格式化快捷键

    Oracle SQL Developer中SQL语句格式化快捷键: 每次sql复制到SQL Developer面板的时候,格式老不对,而且看起来很不舒服,所有的sql都挤在一行完成. 这时我们可以全选 ...

  5. SQL语句优化

    (1)      选择最有效率的表名顺序 ( 只在基于规则的优化器中有效 ) : ORACLE 的解析器按照从右到左的顺序处理 FROM 子句中的表名, FROM 子句中写在最后的表 ( 基础表dri ...

  6. LinqToDB 源码分析——生成与执行SQL语句

    生成SQL语句的功能可以算是LinqToDB框架的最后一步.从上一章中我们可以知道处理完表达式树之后,相关生成SQL信息会被保存在一个叫SelectQuery类的实例.有了这个实例我们就可以生成对应的 ...

  7. 年终巨献 史上最全 ——LINQ to SQL语句

    LINQ to SQL语句(1)之Where 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子句.Where操 ...

  8. LINQ to SQL语句(19)之ADO.NET与LINQ to SQL

    它基于由 ADO.NET 提供程序模型提供的服务.因此,我们可以将 LINQ to SQL 代码与现有的 ADO.Net 应用程序混合在一起,将当前 ADO.NET 解决方案迁移到 LINQ to S ...

  9. LINQ to SQL语句(17)之对象加载

    对象加载 延迟加载 在查询某对象时,实际上你只查询该对象.不会同时自动获取这个对象.这就是延迟加载. 例如,您可能需要查看客户数据和订单数据.你最初不一定需要检索与每个客户有关的所有订单数据.其优点是 ...

随机推荐

  1. maven org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 60

      maven org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant poo ...

  2. 别人的spring学习入门笔记

    http://elf8848.iteye.com/blog/875830  可以做入门参考,英语好可以阅读spring的spring-framework-reference 更多学习blog http ...

  3. Eclipse c++ 编译调试

    直接添加源文件方法: 右键选择工程->import->General->File System,在弹出的对话框中选择源文件目录,筛选文件后: 1.如果直接加到工程中,点Finish就 ...

  4. My Sql 高效分页

    /* *普通分页 *在数据文件上偏移1000000查出10条 */ select * from zoldesk_92game_net_ecms_bj where classid=303 ORDER B ...

  5. springmvc编码问题

    web.xml中加入 <filter> <filter-name>encodingFilter</filter-name> <filter-class> ...

  6. HDUOJ----4509湫湫系列故事——减肥记II

    湫湫系列故事——减肥记II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  7. PAT 1087 All Roads Lead to Rome

    PAT 1087 All Roads Lead to Rome 题目: Indeed there are many different tourist routes from our city to ...

  8. DPDK架构与特点

    当年在某公司实习的时候,当时老大给了我一份DPDK的文档,说是将来很有用,熟悉DPDK对能力提高也有帮助,就试着翻译了 <Intel DPDK Getting Started Guide> ...

  9. 由ConcurrentLinkedQueue扯到线程安全 待整理

    前几天项目总是报错,找了下原因. ConcurrentLinkedQueue 本身是一个基于链接节点的无界线程安全队列,你自己调用就不用考虑线程安全了吗? 结论是:原子性操作当然是线程安全的,非原子性 ...

  10. JFinal record类型数据在前台获取

    1.jfinal record还得自己处理一下 可以使用 this.setSessionAttr("user", record.getColumns()); 这样在jsp中el表达 ...