doutangliang7769 2010-09-13 13:25
浏览 54
已采纳

MySQL SELECT MIN一直有效,但只有在BETWEEN日期时才返回

I can certainly do this by iterating through results with PHP, but just wanted to know if someone had a second to post a better solution.

The scenario is that I have a list of transactions. I select two dates and run a report to get the transactions between those two dates...easy. For one of the reporting sections though, I need to only return the transaction if it was their first transaction.

Here was where I got with the query:

 SELECT *, MIN(bb_transactions.trans_tran_date) AS temp_first_time 
 FROM 
   bb_business      
   RIGHT JOIN bb_transactions ON bb_transactions.trans_store_id = bb_business.store_id 
   LEFT JOIN bb_member ON bb_member.member_id = bb_transactions.trans_member_id 
 WHERE 
   bb_transactions.trans_tran_date BETWEEN '2010-08-01' AND '2010-09-13' 
   AND bb_business.id = '5651' 
 GROUP BY bb_member.member_id 
 ORDER BY bb_member.member_id DESC

This gives me the MIN of the transactions between the selected dates. What I really need is the overall MIN if it falls between the two dates. Does that make sense?

I basically need to know if a customers purchased for the first time in the reporting period.

Anyways, no huge rush as I can solve with PHP. Mostly for my own curiosity and learning.

Thanks for spreading the knowledge!

EDIT: I had to edit the query because I had left one of my trial-errors in there. I had also tried to use the temporary column created from MIN as the selector between the two dates. That returned an error.

SOLUTION: Here is the revised query after help from you guys:

SELECT * FROM (
  SELECT 
    bb_member.member_id, 
    MIN(bb_transactions.trans_tran_date) AS first_time
  FROM 
    bb_business 
    RIGHT JOIN bb_transactions ON bb_transactions.trans_store_id = bb_business.store_id 
    LEFT JOIN bb_member ON bb_member.member_id = bb_transactions.trans_member_id 
  WHERE bb_business.id = '5651' 
  GROUP BY bb_member.member_id
) AS T 
WHERE T.first_time BETWEEN '2010-08-01' AND '2010-09-13'
  • 写回答

1条回答 默认 最新

  • drus40229 2010-09-13 13:43
    关注

    If we do a minimum of all transactions by customer, then check to see if that is in the correct period we get something along the lines of...

    This will simply give you a yes/no flag as to whether the customer's first purchase was within the period...

    SELECT CASE COUNT(*) WHEN 0 THEN 'Yes' ELSE 'No' END As [WasFirstTransInThisPeriod?]
    FROM (  
            SELECT bb_member.member_id As [member_id], MIN(bb_transactions.trans_tran_date) AS temp_first_time 
            FROM bb_business      
            RIGHT JOIN bb_transactions ON bb_transactions.trans_store_id = bb_business.store_id 
            LEFT JOIN bb_member ON bb_member.member_id = bb_transactions.trans_member_id 
            WHERE bb_business.id = '5651' 
            GROUP BY bb_member.member_id
        ) T
    WHERE T.temp_first_time BETWEEN '2010-08-01' AND '2010-09-13'
    ORDER BY T.member_id DESC
    

    (this is in T-SQL, but should hopefully give an idea of how this can be achieved similarly in mySQL)

    Simon

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)