简体   繁体   English

MySQL SELECT MIN一直有效,但仅在日期之间返回

[英]MySQL SELECT MIN for all time, but only return if BETWEEN dates

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. 我当然可以通过遍历PHP的结果来做到这一点,但只是想知道是否有人愿意发布更好的解决方案。

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. 这使我有MIN选定日期之间的交易。 What I really need is the overall MIN if it falls between the two dates. 我真正需要的是总体MIN如果它介于两个日期之间。 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. 无论如何,用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. 我还尝试过使用从MIN创建的临时列作为两个日期之间的选择器。 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'

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) (这是在T-SQL中,但是希望可以对如何在mySQL中同样实现这一点有所了解)

Simon 西蒙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM