首页 > MYSQL数据库能在view中写多表查询吗

MYSQL数据库能在view中写多表查询吗

MYSQL数据库能在view中写多表查询吗


多表查询是个很基础的操作很多数据库都支持的


SQL的VIEW都支持多表查询的
例子如下

 //创建商品表PRODUCT
 CREATE TABLE product(product_id int not null, name varchar(50) not null,price double not null);
//插入商品数据Apple
INSERT INTO product values(1,'apple',5.5);
//创建购买表PURCHASE
CREATE TABLE purchase(id int not null, product_id int not null, qty int not null default 0, gen_tim datetime not null);
//插入购买数据
INSERT INTO purchase values(1,1,10,now());
//创建购买详情视图
CREATE VIEW purchase_detail as 
SELECT product.name as name,
       product.price as price,
       purchase.qty as qty,
       product.price * purchase.qty as total_value 
FROM product,purchase 
WHERE product.product_id=purchase.product_id;
//查询购买详情视图purchase_detail
SELECT * from purchase_detail;
【热门文章】
【热门文章】