09/10/2018, 23:17
Lay 5 Invoice gan nhat cho tung Khach hang. Help???
Lay 5 Invoice gan nhat cho tung Khach hang. Help???
--------------------------------------------------------------------------------
Toi co 2 tablle:
- Customer(CustID,CustName,Address)
- Invoice(CustID,InvoiceID,InvoiceDate)
=> cho toi hoi Cau query the nao de lay duoc danh sach 5 invoices gan nhat (tinh theo InvoiceDate) cua tung Khach hang mot (chu y: khong phai 5 invoice gan nhat tren table Invoice)
--------------------------------------------------------------------------------
Toi co 2 tablle:
- Customer(CustID,CustName,Address)
- Invoice(CustID,InvoiceID,InvoiceDate)
=> cho toi hoi Cau query the nao de lay duoc danh sach 5 invoices gan nhat (tinh theo InvoiceDate) cua tung Khach hang mot (chu y: khong phai 5 invoice gan nhat tren table Invoice)
Bài liên quan
select InvoiceID from Invoice where CustID=$CustID order by InvoiceDate Desc limit 0,5
Desc limit 0,5 ???
Ban oi, toi chua hieu duoc cach lam cua ban. Ban giai thich ro hon dum, thanks so much!
=> query của bạn thiếu mất bảng Customer đúng không?
select InvoiceID from Invoice,Customer
where Customer.CustID = Invoice.CustID order by InvoiceDate Desc limit 0,5
Còn "limit 0,5" là gì? Bạn giải thích giúp tôi ý nghĩa của nó? Nó là từ khóa trong SQL ư?
select top 5 orderID from orders where CustomerID='HANAR' order by OrderDate desc
------------------------------------------------------------------
Bạn thử 2 câu query này trên CSDL Northwind sẽ thấy ngay
Còn cái limit 0,5 của bác Darkmind đúng là em mới thấy lần đầu, hình như nó kô phải là từ khóa trong SQL 2000
select top 5 orderID from orders where CustomerID='HANAR' order by OrderDate desc
Để lấy ra 5 hóa đơn mới nhất của mỗi khách hàng, câu lệnh sẽ là:
SELECT c.CustID, c.CustName, i.InvoiceDate FROM Customer c, Invoice i
WHERE (c.CustID = i.CustID) AND (i.InvoiceID IN (SELECT TOP 2 InvoiceID FROM Invoice j WHERE (c.CustID = j.CustID) ORDER BY j.InvoiceDate DESC))
ORDER BY c.CustID, i.InvoiceDate DESC
Câu lệnh trên sử dụng cho MS SQL Server. Nếu Oracle thì có thể sử dụng ROWNUM thay cho TOP và chuyển ROWNUM vào WHERE