Board logo

標題: Simple SQL question [打印本頁]

作者: luckiejacky    時間: 2013-9-8 11:23     標題: Simple SQL question

  1. select count(c.CustomerID)
  2.   from customers AS c INNER JOIN orders AS o
  3.   ON (c.CustomerID = o.CustomerID)
  4.     where DATE(o.PurchaseDateTime) BETWEEN CURDATE() - INTERVAL 1 MONTH AND CURDATE()
  5.      group by c.CustomerID
複製代碼
This results in every CustomerID joining with every order
So if I have 6 customers
The results will be
6
1
1
1
1
1
How can I only get the first number, but I need o.PurchaseDateTime
Because I want to include the range of dates in the query.
Thanks
作者: 杜龍    時間: 2013-9-8 13:05

select ..... order by ? limit 1, 1?
作者: KinChungE    時間: 2013-9-8 15:46

select ..... order by ? limit 1, 1?
杜龍 發表於 2013-9-8 13:05


+1, 先order by再limit
(假設你用緊MySQL)
作者: 神秘二代    時間: 2013-9-8 20:53

加個max唔得?
select max(count(c.CustomerID)) ......
作者: franklee006    時間: 2013-9-9 09:58

select ..... order by ? limit 1, 1?
杜龍 發表於 2013-9-8 13:05



    你1, 1 會選左第一個, 應該係 0, 1





歡迎光臨 電腦領域 HKEPC Hardware (https://h0.hkepc.com/forum/) Powered by Discuz! 7.2