MySQL查询

查询语句

select [字段列表、*]  from 表名称  [where]

[order by 字段  asc升序|desc降序][limit 起始位置,长度][group by 分组 [having]]

1> select * from 表名     


2> select 字段1,字段2,...... from 表名

select bName , price ,publishing from books     

字段和表名 都可以起别名select bName as bn,price as pr ,publishing as pb from books as bo;

3>order by( asc升序|desc降序)  

select * from books order by price asc;  select * from books order by price desc ;

4>limit  起始位置,长度

select * from books limit 1, 3;

5>group by 分组  

select * from books group by bTypeId;

6>count 统计记录数据    

select btypeid,count(*)  from books group by btypeid;

7>where 查询条件  

比较查询:查询条件 可以写所有的比较表达式 (> < >= <=    =(比较)  <> != )

select * from books where price>  >=  <= <>45  查找所有书籍 条件:价格大于45
逻辑运算:mysql操语句    and(&&)     or(|)
select * from books where price=45 or price=50 查找价格 =45  或  =50 书籍
select * from books where price >45 and price<50  查找价格在 45 到50之间图书
模糊查询:查找字段中 含有 关键词的结果 % (匹配任意长度的字符串)  _ (匹配一个字符)
  select bName,price from books where bName like "%网%"
关键字查询:
[not] between .... and....    (连续的区间)
select * from books where price [not] between 45 and 50;  查找价格 45--50之间书  注意:包含边界值
[not] in 查找的是不连续的区间
select * from books where price [not] in(45,46,47,48);
8>子查询:嵌套式查询

select * from books where  字段  运算符|关键字  (select 语句)

select * from books where btypeid=(select btypeid from btype where btypename="平面")   效率比较低
9> 连接查询:内链接(等值连接)

select * from 表1 INNER JOIN 表2 ON 表1.字段 = 表2.字段 -> 或select * from 表1,表2 where  表1.字段 = 表2.字段;

select * from books,btype where books.btypeid=btype.btypeid;
10>外连接 
以一个共有的字段,求多张表 并集,且把 多张表连接起来
左外链接
格式:
 select *  from 主表 left join 从表 on 连接条件  [where 查询条件]
主表的数据全部都会显示  
查找所有图书的信息 ,包括类型信息
select * from books  as bo  left join  btype as bt on bo.btypeid=bt.btypeid  
注意:
主表 中内容 全部显示
从表中  有对应主表内容的 显示 ,没有的 显示null
逻辑错误: 能执行出来 但是结果不对   字符串 
右外链接
select *  from 从表 right join 主表 on 连接条件  [where 查询条件]
select * from btype  as bt  right join  books as bo on bo.btypeid=bt.btypeid
主表 中内容 全部显示
从表中  有对应主表内容的 显示 ,没有的 显示null
逻辑错误: 能执行出来 但是结果不对   字符串 
posted @ 2015-03-21 11:16  havoe  阅读(225)  评论(0编辑  收藏  举报