Kho Bài Lab
Lab05:
Đề bài:
Bài làm:
CREATE DATABASE /*!32312 IF NOT EXISTS*/`1812767_database_lab05` /*!40100 DEFAULT CHARACTER SET latin1 */;USE `1812767_database_lab05`;/*************************************************************** *//**************************Bài Làm ***************************** *//*************************************************************** */select * from `customers`;select * from `employees`;select * from `offices`;select * from `orders`;select * from `orderdetails`;select * from `products`;select * from `productlines`;/*Đưa ra tên các thành phố và số lượng khách hàng tại từng thành phố. */select city,count(city) as SoLuongKhachHangfrom `customers`group by city;/*Lấy ra thông tin thành phố có số lượng khách hàng lớn nhất */select city,count(city) as SoLuongKhachHangfrom `customers`group by cityhaving count(city) = (select count(city)from `customers`group by cityorder by count(city) desclimit 1);/*Tìm thành phố của khách hàng có nhiều đơn hàng nhất trong năm 2005. */select city,count(`orders`.customerNumber) as TongDHfrom `customers`,`orders`where `customers`.customerNumber = `orders`.customerNumberand EXTRACT(YEAR FROM `orders`.orderDate) = 2005group by cityhaving count(`orders`.customerNumber) = (select count(`orders`.customerNumber)from `customers`,`orders`where `customers`.customerNumber = `orders`.customerNumberand EXTRACT(YEAR FROM `orders`.orderDate) = 2005group by cityorder by count(`orders`.customerNumber) desclimit 1);/*Đưa ra số lượng các đơn đặt hàng trong tháng 5/2003*/select * from `orders`;select count(*) TongSoCacDonHangThang5_2003from `orders`where EXTRACT(year from orderDate) = 2003 and EXTRACT(monthfrom orderDate) = 5;/*Liệt kê họ tên khách hàng cùng số lượng các đơn bị hủy của họ trong năm 2003 */select `customers`.customerName,`orders`.orderDate,`orders`.requiredDate,`orders`.shippedDate,`orders`.status,count(`orders`.customerNumber) as SoDonBiHuyfrom `customers`,`orders`where `customers`.customerNumber = `orders`.customerNumberand EXTRACT(year from orderDate) = 2003and `orders`.status like 'Cancelled'group by `customers`.customerName;/*Đưa ra số lượng các đơn đặt hàng trong từng tháng của năm 2004. Nếutháng nào có số lượng nhỏ nhất thì xuất ra tháng đó cùng với họ tên củakhách hàng đã đặt */select `customers`.customerName,orderDate,count(month(orderDate)) as SoDonHangfrom `customers`,`orders`where `customers`.customerNumber = `orders`.customerNumberand EXTRACT(year from orderDate) = 2004group by month(orderDate)having count(month(orderDate)) = (select count(month(orderDate))from `customers`,`orders`where `customers`.customerNumber = `orders`.customerNumberand EXTRACT(year from orderDate) = 2004group by month(orderDate)order by count(month(orderDate)) asclimit 1);/*Đưa ra các mã đơn đặt hàng có giá trị lớn nhất của dòng sản phẩm “Classic Cars”. */select orderNumber,(quantityOrdered * priceEach) as GiaTrifrom `products`,`orderdetails`where `products`.productCode = `orderdetails`.productCodeand productLine like 'Classic Cars'order by (quantityOrdered * priceEach) desclimit 1;/*Đưa ra mã nhóm hàng, tên nhóm hàng và tổng số lượng hàng hoá còn trongkho của nhóm hàng đó */select * from `products`;select productCode,productName,productLine,quantityInStockfrom `products`group by productCodeorder by quantityInStock desc;/*Đưa ra thông tin về các nhân viên và tên văn phòng nơi họ làm việc.*/select employeeNumber,firstName,lastName,email,jobTitle,city,phone,addressLine1,addressLine2state,countryfrom `employees`inner join `offices`on `employees`.officeCode=`offices`.officeCode;/*Đưa ra thông tin về tên khách hàng và tên các sản phẩm họ đã mua có chứachữ “Ford” nhưng không chứa chữ “A”.*/-- cách 1select `customers`.customerNumber,`customers`.customerName,`customers`.contactLastName,`customers`.contactFirstName,`products`.productCode,`products`.productName,`products`.productLinefrom `customers`INNER JOIN `orders` ON `customers`.customerNumber = `orders`.customerNumberINNER JOIN `orderdetails` ON `orders`.orderNumber = `orderdetails`.orderNumberINNER JOIN `products` ON `orderdetails`.productCode = `products`.productCodewhere productName like '%Ford%'and productName not like '%A%';-- cách 2select `customers`.customerNumber,`customers`.customerName,`customers`.contactLastName,`customers`.contactFirstName,`products`.productCode,`products`.productName,`products`.productLinefrom `customers`,`orders`,`orderdetails`,`products`where `customers`.customerNumber = `orders`.customerNumberand `orders`.orderNumber = `orderdetails`.orderNumberand `orderdetails`.productCode = `products`.productCodeand productName like '%Ford%'and productName not like '%A%';/*Đưa ra thông tin về các mặt hàng chưa có ai đặt mua trong năm 2005 nhưngcó đặt trong năm 2004*/select `orders`.orderNumber,`orders`.orderDate,`products`.productCode,`products`.productName,`products`.productLine,`products`.productVendorfrom `orders`INNER JOIN `orderdetails` ON `orders`.orderNumber = `orderdetails`.orderNumberINNER JOIN `products` ON `orderdetails`.productCode = `products`.productCodewhere year(orderDate) = 2004 and year(orderDate) != 2005;/* Đưa ra các đơn hàng trong tháng 5/2005 (gồm orderDate, requiredDate,Status) và tổng giá trị của mỗi đơn hàng lớn hơn 10 000 nhưng không vượtquá 50 000*/select orderDate,requiredDate,status,(quantityOrdered * priceEach) as TongGTfrom `orders`INNER JOIN `orderdetails` ON `orders`.orderNumber = `orderdetails`.orderNumberwhere month(orderDate) = 5and year(orderDate) = 2005and (quantityOrdered * priceEach) > 10000and (quantityOrdered * priceEach) < 50000;/* Đưa ra thông tin về các dòng sản phẩm và số lượng sản phẩm của dòng sảnphẩm đó. Sắp xếp theo thứ tự số lượng giảm dần.*/select `productlines`.productLine,`productlines`.textDescription,count(`products`.productLine) as SoLuongSPfrom `productlines`INNER JOIN `products` ON `products`.productLine = `productlines`.productLinegroup by `products`.productLineorder by SoLuongSP desc;/*Đưa ra thông tin về các sản phẩm của dòng sản phẩm Motorcycles mà sốký tự trong tên sản phẩm không vượt quá 25. */select productCode,SUBSTRING(productName, 1, 25) AS productName,productLine,productVendor,productDescriptionfrom `products`;/*Đưa ra thông tin về nhân viên mà văn phòng nơi họ làm việc có tổng cácchữ số thành phần của số điện thoại là lớn nhất*/select * from `employees`;select * from `offices`;/*************************************************************** *//**************************Hoàn thành bài Làm ***************************** *//*************************************************************** */

0 Nhận xét