By GokiSoft.com|
21:35 15/12/2023|
SQL Server/MySQL
SQL - Thiết kế CSDL quản lý bán hàng - Lập trình SQL Server
Yêu cầu.
1.Thiết kế bảng hàng hóa gồm các column sau- id kiểu số nguyên, khóa chính, tự tăng- tên mặt hàng- nhà sản xuất- xuất xứ (Việt Nam, Japan, ...)- giá nhập- giá bán- ngày sản xuất : kiểu dữ liệu date->Thực hiện chèn them 10 bản ghi vào bảng2. Thiết kế bảng bán hàng gồm các column sau- id đơn hàng kiểu số nguyên, khóa chính, tự tăng- id_hanghoa khóa ngoai liên kết vs khóa id của bang hàng hóa- chú thich : dùng ghi lại cho mỗi đơn hàng- ngày bán : kiểu date- số lượng
->Thực hiện nhập 10 bản ghi cho bảng này
3. Thực hiện liệt kê tất cả các đơn hàng đã được bán ra4. Liệt kê các đơn hàng được bán ra có xuất xứ Việt Nam 5. Thống kê tổng giá bán được cho từng mặt hàng.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Vũ Đình Khôi [community]](https://www.gravatar.com/avatar/522a3ab049e7409705e97b96dbbc327b.jpg?s=80&d=mm&r=g)
Vũ Đình Khôi
2020-11-30 06:56:32
#BT1776.sql
create database quanlybanhang
use quanlybanhang1776
create table hanghoa (
id int primary key identity(1,1),
tenmathang nvarchar(50),
tennhasanxuat nvarchar(50),
xuatxu nvarchar(20),
gianhap float,
giaban float,
ngaysx date
)
create table banhang (
id int primary key identity(1,1),
id_hoanghoa int references hanghoa(id),
note nvarchar(500),
ngayban date,
soluong int
)
select * from hanghoa
select * from banhang
insert into hanghoa (tenmathang, tennhasanxuat, xuatxu, gianhap, giaban, ngaysx)
values
('Tu lanh', 'Sony', 'VIET NAM', 10000000, 12000000, '2020-06-27'),
('Tu Lanh', 'Samsung', 'KOREA', 9000000, 10000000, '2020-04-27'),
('May Hut Bui', 'LG', 'VIET NAM', 7000000, 8000000, '2020-08-26'),
('Dieu Hoa', 'LG', 'VIET NAM', 11000000, 12000000, '2020-11-26'),
('May Giat', 'Sony', 'VIET NAM', 4000000, 5000000, '2020-13-23'),
('May Giat', 'Sony', 'JAPAN', 8900000, 9000000, '2020-1120-23'),
('Tivi', 'LG', 'VIET NAM', 10000000, 12500000, '2020-11-24'),
('Tivi', 'Samsung', 'JAPAN', 11000000, 12700000, '2020-50-24')
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2020-11-29 06:29:26
create database bt1776
use bt1776
create table hanghoa (
id int primary key identity(1,1),
name nvarchar(100),
producer nvarchar(100),
origin nvarchar(100),
import_price float,
price float,
manufacture date
)
create table banhang (
id_order int primary key identity(1,1),
id_hanghoa int references hanghoa(id),
note nvarchar(200),
date_sale date,
amount int
)
insert into hanghoa(name, producer, origin, import_price, price, manufacture)
values
('rau cai', 'nsx1', 'viet nam', '1000', '2000', '2020-11-26' ),
('ca rot', 'nsx2', 'han quoc', '2000', '5000', '2020-11-26' ),
('lua mi', 'nsx3', 'nhat ban', '1500', '3000', '2020-11-26'),
('gao nep', 'nsx4', 'viet nam', '3000', '4000', '2020-11-26'),
('ca chep', 'nsx5', 'viet nam', '3500', '6000', '2020-11-26'),
('ca chua', 'nsx6', 'campuchia', '2500', '4000', '2020-11-26'),
('dau tay', 'nsx7', 'han quoc', '3000', '6000', '2020-11-26'),
('tom hum', 'nsx8', 'nga', '4000', '8000', '2020-11-26'),
('cua hoang de', 'nsx9', 'viet nam', '5000', '9000', '2020-11-26'),
('bao ngu', 'nsx10', 'nhat ban', '3500', '4000', '2020-11-26')
insert into banhang(id_hanghoa, note, date_sale, amount)
values
('1', '', '2020-11-27', '1'),
('2', '', '2020-11-27', '2'),
('3', '', '2020-11-27', '1'),
('4', '', '2020-11-27', '5'),
('5', '', '2020-11-27', '6'),
('6', '', '2020-11-27', '3'),
('7', '', '2020-11-27', '10'),
('8', '', '2020-11-27', '4'),
('9', '', '2020-11-27', '2'),
('10', '', '2020-11-27', '0')
select * from banhang
--cac don hang dc ban ra
select * from hanghoa
--cac don hang co xuat xu viet nam
select * from hanghoa
where origin='viet nam'
--thong ke tong gia ban
select hanghoa.name, banhang.amount, hanghoa.price
into thongke
from hanghoa, banhang
where hanghoa.id = banhang.id_hanghoa
select * from thongke
alter table thongke
add tong_gia float
update thongke set tong_gia = amount * price
![Nguyễn Xuân Mai [T2008A]](https://www.gravatar.com/avatar/d3d863d6f47708501814fb41e9c38f31.jpg?s=80&d=mm&r=g)
Nguyễn Xuân Mai
2020-11-29 03:36:31
create database quanlybanhang_2
use quanlybanhang_2
create table product(
id int primary key identity(1,1),
name nvarchar(100),
producer nvarchar(100),
made_in nvarchar(100),
import_price float,
sell_price float,
manu_date date
)
go
insert into product(name, producer, made_in, import_price, sell_price, manu_date)
values
('ao khoac','abc','japan', 252, 100635, '2020/11/25'),
('ao phong','abc','vietnam', 100000, 250536, '2020/04/06'),
('vay','abc','singapore', 260000, 206573, '2017/12/22'),
('quan bo','abc','vietnam', 520000, 76005635, '2016/11/29'),
('quan dui','abc','vietnam', 8000, 12034, '2019/04/18'),
('ao dai tay','abc','japan', 34562, 456725, '2020/11/27'),
('ao giu nhiet','abc','korea', 56762, 897245, '2019/10/24'),
('chan vay','abc','vietnam', 345600, 6734000, '2020/08/28'),
('vest','abc','vietnam', 650000, 7600000, '2015/12/25'),
('quan kaki','abc','korea', 65700, 567300, '2020/01/21')
select * from product
create table sale(
id int primary key identity(1,1),
id_hanghoa int references product(id),
note text,
sale_date date,
amount int
)
go
insert into sale(id_hanghoa, note, sale_date, amount)
values
(4,'best seller', '2020/11/05',56),
(5,'newly arrived', '2020/11/27',65),
(3,'out of stock', '2020/11/23',496),
(7,'80 left', '2020/11/25',26),
(6,'newly arrived', '2020/11/27',85),
(8,'380 left', '2020/10/18',998),
(9,'346 left', '2020/09/24',57),
(10,'best seller', '2020/10/29',89),
(3,'newly arrived', '2020/08/30',564),
(2,'newly arrived', '2020/11/25',28)
select * from sale
select product.id, name, producer, made_in, sell_price, manu_date, note, sale_date, amount
from product, sale
where product.id= sale.id_hanghoa and product.made_in= 'Vietnam'
alter table sale
add total_price float
select sale.id, id_hanghoa, name, note, sale_date, amount, sell_price, total_price into don_hang
from sale, product
where product.id=sale.id_hanghoa
select * from don_hang
update don_hang set total_price=amount*sell_price
![Đức Sơn [T2008A]](https://www.gravatar.com/avatar/d2b971b7bc54e4a9689c3e2240f27949.jpg?s=80&d=mm&r=g)
Đức Sơn
2020-11-28 09:32:04
---bang hang hoa---
create table sales (
id int identity(1,1) primary key,
name nvarchar(100),
producer nvarchar(50),
madein nvarchar(50),
import_price float,
price float,
date_sx date
)
go
---bang ban hang---
create table sales_table (
id int identity(1,1) primary key,
note nvarchar(200),
date_sell date,
num int
)
go
alter table sales_table
add constraint fk_id_sp foreign key(id_sales) references sales(id)
insert into sales(name,producer,madein,import_price,price,date_sx)
values
('ao','abc','Made in Vietnam','200.000','280.000','2020/08/15'),
('quan','bcd','Made in Japan','300.000','350.000','2019/03/10'),
('giay','abc1','Made in USA','500.000','600.000','2019/04/21'),
('dep','abc2','Made in Laos','100.000','150.000','2018/08/14'),
('mu','abc2','Made in Thailand','150.000','220.000','2019/05/17'),
('that lung','abc4','Made in Myanmar','100.000','150.000','2018/11/25'),
('khan','abc5','Made in Campuchia','100.000','140.000','2019/10/28'),
('tat','abc6','Made in Portugal','50.000','80.000','2019/08/12'),
('chan','abc7','Made in Israel','350.000','410.000','2020/03/11'),
('dem','abc8','Made in Brazil','400.000','460.000','2019/04/29')
select * from sales
insert into sales_table(note, date_sell, num)
values
(1,'abc1','2020-04-19 13:30','5'),
(2,'abc2','2020-04-19 13:30','10'),
(3,'abc3','2020-04-19 13:30','9'),
(5,'abc4','2020-04-19 13:30','8'),
(4,'abc5','2020-04-19 13:30','15'),
(6,'abc6','2020-04-19 13:30','30'),
(7,'abc7','2020-04-19 13:30','20'),
(8,'abc8','2020-04-19 13:30','17'),
(9,'abc9','2020-04-19 13:30','45'),
(10,'abc10','2020-04-19 13:30','38')
select * from sales_table
---Liên Kết các đơn hàng đã đươc bán ra---
select name, producer, madein, import_price, price, date_sx, note, date_sell, num
from sales, sales_table
where sales.id= sales_table.id
---Liệt kê các đơn hàng được bán có xuất xứ Việt Nam---
select name, producer, madein, import_price, price, date_sx, note, date_sell, num
from sales, sales_table
where sales.id= sales_table.id and sales.madein= 'Vietnam'
---Thống kê tổng giá bán được cho từng mặt hàng---
select tung_mat_hang, Sum(num) as tong_gia_ban
from sales, sales_table
group by tung_mat_hang
order by tong_gia_ban asc
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2020-11-28 06:35:49
create database gokisoft_buoi5_quanlybanhangthoitrang
use "gokisoft_buoi5_quanlybanhangthoitrang"
create table product(
id int identity(1,1),
productName nvarchar(100),
manufacture nvarchar(100),
country nvarchar(100),
price_in float,
prrice_out float,
ManufacturingDate date,
constraint pk_product primary key (id)
)
alter table product
drop column prrice_out
alter table product
add price_out int
create table sale(
id int identity(1,1),
id_hanghoa int,
note nvarchar(300),
sale_day date,
quantity int,
constraint fk_id_hanghoa foreign key (id_hanghoa) references product(id)
)
select * from product
--Nhap du lieu
insert into product (productName,manufacture,country,price_in,price_out,ManufacturingDate)
values
('Giày công sở Oxford Model 341','Oxford','England','800','1200','2020-11-30'),
('Giày công sở Lanbobi Model 453','Lanbobi','England','300','500','2020-11-30'),
('Giày thể thao Thượng Đình','Thượng Đình','Việt Nam','2','5','2020-10-10'),
('Giày thể thao Nike Model 132','Nike','American','2','5','2020-10-10'),
('Giày bóng đá V121','TrungDuc Sport','Việt Nam','10','18','2020-7-10'),
('Giày leo núi LN03','VanDiep Sport','Việt Nam','8','12','2020-7-15'),
('Giày công sở CS25','TienDat Shoes','Việt Nam','15','22','2019-6-15'),
('Giày thể thao A27','Adidas','Germany','30','41','2020-8-15'),
('Giày leo núi M8','Adidas','Germany','17','28','2019-8-15'),
('Giày leo núi Nike MN12','Nike','American','40','45','2020-9-12')
insert into sale (id_hanghoa,note,sale_day,quantity)
values
(1,'','2020-11-27','7'),
(2,'','2020-11-27','2'),
(3,'','2020-11-27','3'),
(4,'','2020-11-27','5'),
(5,'','2020-11-27','4'),
(6,'','2020-11-27','6'),
(7,'','2020-11-27','6'),
(8,'','2020-11-27','5'),
(9,'','2020-11-27','2'),
(10,'Hàng rất ế','2020-11-28','1'),
(1,'','2020-11-28','2'),
(2,'','2020-11-28','1'),
(3,'','2020-11-28','4'),
(4,'','2020-11-28','6'),
(5,'','2020-11-28','2'),
(6,'','2020-11-28','9'),
(7,'','2020-11-28','4'),
(8,'','2020-11-28','6'),
(9,'','2020-11-28','5'),
(10,'Hàng rất ế','2020-11-28','0')
select * from product
select * from sale
select * from sale where quantity > 0
select sale.id, sale.id_hanghoa, sale.note, sale_day, sale.quantity, product.country, product.productName
from sale inner join product
on country = 'Việt Nam' and sale.id_hanghoa = product.id
select id_hanghoa , sum(quantity) as tongsoluong
from sale
group by id_hanghoa
select id_hanghoa , sum(quantity) as tongdongia
from sale
group by id_hanghoa
select sale.id, sale.id_hanghoa, sale.note, sale_day, sale.quantity, product.price_out into bill
from product, sale
where sale.id_hanghoa = product.id
select * from bill
alter table bill
add total_bill_each_id_hanghoa float
update bill
set total_bill_each_id_hanghoa = quantity*price_out
select id_hanghoa ,sum(quantity) as'Tong so san pham ban duoc moi san pham',sum(total_bill_each_id_hanghoa) as'Tong tien thu duoc cho moi san pham'
from bill
group by id_hanghoa
having sum(total_bill_each_id_hanghoa) > 1000
![Nguyên Phấn Đông [T2008A]](https://www.gravatar.com/avatar/c9c4f8f79ce35b9224637b6cc5fbe5c4.jpg?s=80&d=mm&r=g)
Nguyên Phấn Đông
2020-11-28 02:05:01
create database bai_tap_2
use bai_tap_2
create table bang_hang_hoa(
id int primary key identity(1,1),
ten_mat_hang nvarchar(100),
nha_san_xuat nvarchar(100),
xuat_xu nvarchar(100),
gia_nhap float,
gia_ban float,
ngay_san_xuat date,
)
go
insert into bang_hang_hoa(ten_mat_hang,nha_san_xuat,xuat_xu,gia_nhap,gia_ban,ngay_san_xuat)
values
('quan ao','dong','viet nam',170000,200000,'2020-01-27 15:01'),
('quan a','dong1','usa',160000,200000,'2020-11-27 15:01'),
('quan au','dong2','viet nam',180000,200000,'2020-11-27 15:01'),
('quan chau','dong3','usb',180000,200000,'2020-07-27 15:01'),
('quan bo','dong4','usa',190000,200000,'2020-11-27 15:01'),
('quan qq','dong5','usc',110000,200000,'2020-11-27 15:01'),
('quan ab','dong6','viet nam',130000,200000,'2020-11-25 15:01'),
('quan abc','dong7','usa',900000,200000,'2020-12-21 15:01'),
('quan abcd','dong8','viet nam',80000,200000,'2020-10-27 15:01'),
('quan abcde','dong9','viet nam',180000,200000,'2020-01-27 15:01')
create table bang_ban_hang(
id int primary key identity(1,1),
id_hanghoa int references bang_ban_hang(id),
chu_thich text,
ngay_ban date,
so_luong int,
)
go
select * from bang_ban_hang
insert into bang_ban_hang(id_hanghoa,chu_thich,ngay_ban,so_luong)
values
(1,'real 1 1','2020-10-27 15:01',2),
(2,'real 1 1','2020-10-27 15:01',12),
(10,'real 1 1','2020-10-27 15:01',30),
(3,'real 1 1','2020-10-27 15:01',20),
(5,'real 1 1','2020-10-27 15:01',15),
(4,'real 1 1','2020-10-27 15:01',7),
(6,'real 1 1','2020-10-27 15:01',2),
(7,'real 1 1','2020-10-27 15:01',6),
(8,'real 1 1','2020-10-27 15:01',4),
(9,'real 1 1','2020-10-27 15:01',80)
select bang_ban_hang.id_hanghoa ,bang_hang_hoa.ten_mat_hang ,bang_hang_hoa.xuat_xu ,bang_hang_hoa.gia_ban,bang_ban_hang.so_luong
into donhang
from bang_ban_hang,bang_hang_hoa
where bang_ban_hang.id_hanghoa=bang_hang_hoa.id
select * from donhang where xuat_xu='viet nam'
alter table donhang
add tong float
update donhang
set tong= so_luong*gia_ban
select * from donhang
select id_hanghoa, ten_mat_hang, sum(tong) as tong_mathang
from donhang
group by id_hanghoa, ten_mat_hang
having sum(tong) > 0
order by tong_mathang asc
![nguyễn Sử [T2008A]](https://www.gravatar.com/avatar/47487be2776ac2ec915b0936ef7ab5ae.jpg?s=80&d=mm&r=g)
nguyễn Sử
2020-11-27 14:25:51
--- bang hang hóa ---
create table hanghoa(
id int identity (1,1) primary key,
name nvarchar (200),
producer nvarchar (200),
madein nvarchar (100),
import_price float,
price float,
date_sx date
)
go
insert into hanghoa (name,producer,madein,import_price,price,date_sx)
values
('ao','2020-11-17','vietnam','200.000','300.000','2020-11-17'),
('quan','2020-11-18','japan','300.000','500.000','2020-11-18'),
('vay','2020-11-19','vietnam','100.000','200.000','2020-11-19'),
('mu','2020-11-20','japan','400.000','600.000','2020-11-20'),
('giay','2020-11-21','USA','500.000','700.000','2020-11-21'),
('dep','2020-11-22','vietnam','100.000','200.000','2020-11-22'),
('abc','2020-11-23','japan','200.000','300.000','2020-11-23'),
('bcd','2020-11-24','vietnam','200.000','300.000','2020-11-24'),
('cde','2020-11-25','USA','200.000','300.000','2020-11-25'),
('efg','2020-11-26','vietnam','200.000','300.000','2020-11-26')
select *from hanghoa
--- bảng bán hàng ---
create table ban_hang(
id int identity (1,1) primary key,
note nvarchar (200),
date_nb date,
num float
)
go
alter table banhang
add constraint fk_id_sp foreign key(id_hanghoa) references hanghoa(id)
insert into ban_hang (note,date_nb,num)
values
('good','2020-11-27','1'),
('good','2020-11-27','1'),
('good','2020-11-27','1'),
('good','2020-11-27','1'),
('good','2020-11-27','1'),
('good','2020-11-27','1'),
('good','2020-11-27','1'),
('good','2020-11-27','1'),
('good','2020-11-27','1'),
('good','2020-11-27','1')
select *from ban_hang
--- Thực hiện liệt kê tất cả các đơn hàng đã được bán ra ---
select name,producer,madein,import_price, price, date_sx,note,date_nb,num
from hanghoa, ban_hang
where hanghoa.id = ban_hang.id
---Liệt kê các đơn hàng được bán ra có xuất xứ Việt Nam ---
select name,producer,madein,import_price, price, date_sx,note,date_nb,num
from hanghoa, ban_hang
where hanghoa.id = ban_hang.id and hanghoa.madein='vietnam'
---Thống kê tổng giá bán được cho từng mặt hàng ---
select tung_mat_hang, SUM(num) as tong_gia_ban
from hanghoa, ban_hang
group by tung_mat_hang
order by tong_gia_ban asc
![Đỗ Mạc Nam [T2008A]](https://www.gravatar.com/avatar/71dd707204cf6115a4fe17bbdee2b5fa.jpg?s=80&d=mm&r=g)
Đỗ Mạc Nam
2020-11-27 13:44:58
create database bai_tap_2
use bai_tap_2
create table bang_hang_hoa(
id int primary key identity(1,1),
ten_mat_hang nvarchar(100),
nha_san_xuat nvarchar(100),
xuat_xu nvarchar(100),
gia_nhap float,
gia_ban float,
ngay_san_xuat date,
)
go
insert into bang_hang_hoa(ten_mat_hang,nha_san_xuat,xuat_xu,gia_nhap,gia_ban,ngay_san_xuat)
values
('giay dep','dong','viet nam',1100000,4000000,'2020-01-27 15:01'),
('nike','dong1','usa',1600000,3200000,'2020-11-27 15:01'),
('adidas','dong2','korea',1100000,2000000,'2020-11-27 15:01'),
('balenciaga','dong3','china',160000,380000,'2020-07-27 15:01'),
('crocs','dong4','china',190000,230000,'2020-11-27 15:01'),
('rich owen','dong5','usa',1900000,4600000,'2020-11-27 15:01'),
('tong lao','dong6','viet nam',13000,20000,'2020-11-25 15:01'),
('to ong','dong7','viet nam',9000,12000,'2020-12-21 15:01'),
('thuong dinh','dong8','viet nam',80000,120000,'2020-10-27 15:01'),
('bitis','dong9','viet nam',1800000,2000000,'2020-01-27 15:01')
create table bang_ban_hang(
id int primary key identity(1,1),
id_hanghoa int references bang_ban_hang(id),
chu_thich text,
ngay_ban date,
so_luong int,
)
go
insert into bang_ban_hang(id_hanghoa,chu_thich,ngay_ban,so_luong)
values
(1,'real 1 1','2020-11-12 11:30',2),
(5,'real 1 1','2020-11-12 11:30',12),
(4,'real 1 1','2020-11-12 11:30',30),
(1,'real 1 1','2020-11-12 11:30',20),
(6,'real 1 1','2020-11-12 11:30',15),
(9,'real 1 1','2020-11-12 11:30',7),
(4,'real 1 1','2020-11-12 11:30',2),
(2,'real 1 1','2020-11-12 11:30',6),
(7,'real 1 1','2020-11-12 11:30',4),
(3,'real 1 1','2020-11-12 11:30',80)
select * from bang_ban_hang
select * from bang_hang_hoa
----Thực hiện liệt kê tất cả các đơn hàng đã được bán ra----.
select bang_ban_hang.id_hanghoa, bang_hang_hoa.ten_mat_hang, bang_hang_hoa.xuat_xu, bang_hang_hoa.gia_ban, bang_ban_hang.so_luong
into donhang
from bang_ban_hang, bang_hang_hoa
where bang_ban_hang.id_hanghoa=bang_hang_hoa.id
----Liệt kê các đơn hàng được bán ra có xuất xứ Việt Nam----.
select * from donhang where xuat_xu='viet nam'
----Thống kê tổng giá bán được cho từng mặt hàng----.
select ten_mat_hang, SUM(num) as tong_gia_ban
from bang_hang_hoa, bang_ban_hang
group by ten_mat_hang
order by tong_gia_ban asc
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2020-11-27 13:28:35
#Lesson4.sql
CREATE DATABASE BT1776
CREATE TABLE product (
id int(5) PRIMARY KEY identity(1,1),
name nvarchar(50),
producers nvarchar(50),
origin nvarchar(20),
price_in nvarchar(20),
price_out nvarchar(20),
created_at date,
)
INSERT INTO product (id, name, producers, origin, price_in, price_out, created_at)
VALUES ('1','abc','a12','Viet Nam', '80', '100', '2020/08/08'),
('2','abc','a12','Nhat Ban', '80', '150', '2020/06/08'),
('3','abc','a12','Viet Nam', '80', '100', '2020/08/08'),
('4','abc','a12','Viet Nam', '80', '500', '2020/08/08'),
('5','abc','a12','Han Quoc', '80', '250', '2020/08/08'),
('6','abc','a12','Viet Nam', '80', '200', '2020/05/08'),
('7','abc','a12','Nhat Ban', '80', '400', '2020/08/08'),
('8','abc','a12','Nhat Ban', '80', '200', '2020/08/08'),
('9','abc','a12','Singapore', '80', '150', '2020/08/08'),
('10','abc','a12','Viet Nam', '80', '100', '2020/08/08')
CREATE TABLE selling (
id int(5) PRIMARY KEY identity(1,1),
id_hang_hoa int(5),
note nvarchar(50),
date_out date,
quantity nvarchar(10)
)
INSERT INTO selling (id, id_hang_hoa, note, date_out, quantity)
VALUES ('P001', '1', 'hang_loai_1', '2020/09/10', '25'),
('P002', '1', 'hang_loai_1', '2020/10/10', '25'),
('P003', '1', 'hang_loai_1', '2020/09/10', '5'),
('P004', '3', 'hang_loai_3', '2020/09/10', '30'),
('P005', '1', 'hang_loai_6', '2020/09/13', '25'),
('P006', '2', 'hang_loai_1', '2020/09/10', '25'),
('P007', '7', 'hang_loai_4', '2020/09/10', '60'),
('P008', '1', 'hang_loai_5', '2020/09/10', '25'),
('P009', '6', 'hang_loai_7', '2020/09/10', '23'),
('P010', '10', 'hang_loai_1', '2020/09/10', '25')
---Liet ke cac don hang duoc ban ra
SELECT name, producers, origin, price_in, price_out, created_at, id, note, date_out, quantity
FROM product, selling
WHERE product.id=selling.id_hang_hoa
---Liet ke ban hang duoc ban ra o Viet Nam
SELECT *
FROM product
WHERE origin='Viet Nam';
---Tong gia ban cho tung mat hang
SELECT name, price_out, quantity
INTO tong_don_hang
FROM product, selling
ALTER TABLE tong_don_hang
ADD total_price float;
WHERE product.id=selling.id_hang_hoa,
UPDATE tong_don_hang
SET total_price=price_out*quantity
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2020-11-27 12:36:50
-----bang san pham
create table product (
id int identity (1,1) primary key,
product_name nvarchar (100),
producer nvarchar (100),
made_in nvarchar (50),
import_price float,
price float,
date_of_manufacture date
)
go
---bang ban hang
create table sell_product (
id int identity (1,1) primary key,
id_hanghoa int,
note text,
date_of_sale date,
num int
)
go
alter table sell_product
add constraint fk_id_sp foreign key (id_hanghoa) references product(id)
insert into product (product_name,producer,made_in,import_price,price,date_of_manufacture)
values
('Ghe hoc','aaa','Viet Nam','100.000','150.000','2020-05-18'),
('Ban hoc','aaa','Thai Lan','100.000','150.000','2020-05-18'),
('Tu sach','aaa','Nhat Ban','100.000','150.000','2020-05-18'),
('Tu de quan ao','aaa','Viet Nam','100.000','150.000','2020-05-18'),
('Gia sach','aaa','Viet Nam','100.000','150.000','2020-05-18'),
('Giuong ngu','aaa','Viet Nam','100.000','150.000','2020-05-18'),
('Ban lam vec','aaa','Thai Lan','100.000','150.000','2020-05-18'),
('Ghe sofa','aaa','Viet Nam','100.000','150.000','2020-05-18'),
('Tu sach','aaa','Nhat Ban','100.000','150.000','2020-05-18'),
('Goi ngu','aaa','Viet Nam','100.000','150.000','2020-05-18')
insert into sell_product (id_hanghoa,note,date_of_sale,num)
values
('1','san pham tot','2020-12-09','1'),
('2','san pham tot','2020-12-09','1'),
('3','san pham tot','2020-12-09','1'),
('4','san pham tot','2020-12-09','1'),
('5','san pham tot','2020-12-09','1'),
('6','san pham tot','2020-12-09','1'),
('7','san pham tot','2020-12-09','1'),
('8','san pham tot','2020-12-09','1'),
('9','san pham tot','2020-12-09','1'),
('10','san pham tot','2020-12-09','1')
select * from product
select * from sell_product
----Thực hiện liệt kê tất cả các đơn hàng đã được bán ra
select product_name,producer,made_in,import_price,price,date_of_manufacture,note,date_of_sale,num
from product,sell_product
where product.id = sell_product.id_hanghoa
----Liệt kê các đơn hàng được bán ra có xuất xứ Việt Nam
select product_name,producer,made_in,import_price,price,date_of_manufacture,note,date_of_sale,num
from product,sell_product
where product.id = sell_product.id_hanghoa and product.made_in ='Viet Nam'
----Thống kê tổng giá bán được cho từng mặt hàng.
select product_name ,SUM(num) as tong_gia_ban
from product,sell_product
group by product_name
order by tong_gia_ban asc