首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

mysql中基准sql语句的用法

2013-04-02 
mysql中标准sql语句的用法 设置级联更新或者删除:add constraint on delete cascade 链接查询:语法格

mysql中标准sql语句的用法

>> 设置级联更新或者删除:add constraint on delete cascade;

>> 链接查询:

语法格式:from TABLE1 join_type TABLE2 [on join_condition] [where query_condition]

join_condition:连接条件;query_condition:查询条件。

* 交叉查询(cross join):不带on字句,返回连接表中所有数据行的笛卡尔积,比如TABLE1有5行数据,TABLE2有7行数据,查询结果包含35(5 * 7)行数据。

交叉连接例子:select * from CUSTOMERS, ORDERS;

* 内连接(inner join):返回连接表中符合连接条件以及查询条件的数据行。

* 外连接,又分为左外连接(left outer join),右外连接(right outer join)。

左外连接:不仅返回连接表中符合连接条件以及查询条件的数据行,也返回左表中仅符合查询条件但不符合连接条件的数据行。

右外连接类似…

>> 子查询:

在select子句或者where子句中又嵌套select查询语句。

select * from CUSTOMERS c where 3 <= (select count(*) from ORDERS o where c.ID=o.CUSTOMERS_ID);

select * from ORDERS o where o.CUSTOMER_ID in (select ID from CUSTOMERS where NAME like ‘MIKE’);

select * from CUSTOMERS c where not exists (select * from ……..)

select NAME, AGE, (select sum(PRICE) from ORDERS where CUSTOMER_ID=1) TOTAL_PRICE from CUSTOMERS where ID=1;

>> 联合查询:

合并两条查询语句的查询结果,去掉其中重复数据行,返回没有重复数据行的查询结果。

select * from CUSTOMERS where AGE < 25 union select * from CUSTOMERS where AGE >=24;

>> 报表查询:

对数据行进行分组统计,语法格式:

[select ...] from … [where ...] [group by ... [having ...]] [order by...]

group by指定分组,having子句设定分组查询条件。使用的聚集函数有:count(), min(), max(), sum(), avg()

查询数目的时候只使用 select count(*) from TABLE1; 不加where条件最快,杜绝使用count(COLUMN)这种形式。

mysql分页查询:select COL from TABLE1 where … LIMIT 100, 1000 (从100位置开始,最多检索1000条数据)

附上一点有用的东西:

navicat10.1.0最新key:

NAVD-IO5R-4VGM-TIZD、NAVL-RK72-URYA-CINR

热点排行