使用using关键字对链接进行简化sql
在SQL/92标准能够使用USING子句对链接条件进行简化,可是只有在查询知足如下两个条件时才能给使用USING进行简化: 一、查询必须是等链接的 二、等链接中的列必须是同名 如:商品表goods表和商品类型表category表中goods的外键和category的主键相同:categoryid并且是等链接,这里能够使用usingast
[sql] select goodsname,categoryname
from goods inner join category
using(categoryid)
在使用using是须要注意如下几个问题select
一、在select子句中只能指定该列名,不能使用表名或别名 二、在using子句中也只能单独使用列名sql语句
对于多与两个表的链接,先看这个例子 [sql] select c.firstName,c.lastName,p.product_name ,pt.product_types_name
from customers c,purchase pr,products p,product_types pt
where c.customer_id=pr.customer_id www.2cto.com
and p.products_id = pr.products_id
and p.product_types_id=pt.product_types_id;查询
使用using对上面的sql语句进行重写 [sql] select c.first_name,c.last_name,p.products_name as product,pt.product_types_name as typesname
from customers c inner join purchases pr
using(customers_id)
inner join products p
using(products_id)
inner join product_types pt
using(product_types_id);co