我有一张表如下
id | name | action_date | user_id
1 | Jhon | 2014-03-1 | 2
2 | Smith | 2014-03-03 | 3
2 | Jhon | 2014-04-2 | 2
3 | Jhon | 2014-06-3 | 2
我想从3月到4月选择所有行动的用户,并在此之后停止执行任何操作?
在这种情况下,Smith
使用user_id 3
我尝试过SQL但没有运气!
我也试过像SQL一样使用mysql_row等效方法,请海这个
SET @row_number = 0;
SELECT
@row_number:=CASE
WHEN @customer_no = user_id THEN
CASE when
DATE(action_date) >'2009-07-23'
AND DATE(action_date) <'2009-07-25'
THEN
@row_number + 1
ELSE 211 END
ELSE 1
END AS Customer_Number,
@customer_no:=user_id as Customers_id,
action_date
FROM
orders
ORDER BY Customers_id;
任何只使用Mysql的想法。
答案 0 :(得分:1)
一种方式:
SELECT name
FROM my_table
GROUP BY name
HAVING SUM(action_date BETWEEN '2014-03-01' AND '2014-04-30')
AND MAX(action_date) <= '2014-04-30'
答案 1 :(得分:1)
SELECT tab.* FROM tab
inner join
( SELECT min(dt) as mindt, max(dt) as maxdt, user_id FROM tab
group by user_id
having mindt >= '2014-03-01' and maxdt <= '2014-04-30' ) t
on
tab.user_id = t.user_id
答案 2 :(得分:1)
您可以使用&#34; NOT IN&#34; , 尝试此查询除了来自另一个表或同一个表的某些值
SELECT table.`user_id` FROM table
WHERE (`action_date` between STR_TO_DATE('01-03-2014','%d-%m-%Y') and STR_TO_DATE('30-04-2014','%d-%m-%Y') )
AND table.`user_id` NOT IN
(SELECT table.`user_id` FROM table
WHERE `action_date` > STR_TO_DATE('30-04-2014','%d-%m-%Y'))
GROUP BY table.`user_id`
或者如果您想在一个查询中创建它:
SELECT min(action_date) as min_date, max(action_date) as max_date, user_id FROM orders_test
where action_date > '2014-03-01'
group by user_id
having min_date >= '2014-03-01' and max_date <= '2014-04-30'
答案 3 :(得分:0)
从tb_name中选择*,其中action_date位于STR_TO_DATE(&#39; 01,03,2014&#39;,&#39;%d,%m,%Y&#39;)和STR_TO_DATE(&#39; 30, 03,2014&#39;%,%m,%Y&#39;);
I assume here that tb_name is the name of your table.
and you can read more on mysql dates
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
答案 4 :(得分:0)
尝试这种方法......那应该......
SELECT user_id
FROM (
SELECT user_id, STR_TO_DATE(action_date,'%Y-%m-%d') as actionDate, RIGHT( GROUP_CONCAT( DATE( STR_TO_DATE(action_date,'%Y-%m-%d') )
SEPARATOR ',' ) , 10 ) AS last_action
FROM `table`
WHERE actionDate > '2014-03-01'
GROUP BY user_id
)last_action_selection
WHERE last_action < '2014-04-30'
答案 5 :(得分:0)
这是查询:
SELECT b.*
FROM orders b # "b" for "between"
LEFT JOIN orders a # "a" for "after"
ON b.user_id = a.user_id
AND '2014-05-01' <= a.action_date # "a" is after May 1st, 2014
WHERE a.user_id IS NULL # there is no "after April 1st"
AND b.action_date BETWEEN '2014-03-01' AND '2014-04-30'
它将orders
表作为b
(“b”代表“between”)与其自身别名为a
(“a”代表“after”)。来自b
的每一行都与来自a
的所有行配对,这些行具有与2014年5月1日之后user_id
相同的action_date
(同一用户的订单)。
LEFT JOIN
确保b
中的所有行都出现在已加入的结果集中。如果在b
中找不到匹配的行,则会使用一整行NULL
值。
where条件b.action_date BETWEEN '2014-03-01' AND '2014-04-30'
仅保留2014年3月和4月发生的b
的订单,条件a.user_id IS NULL
过滤掉所有在联接中有对的行(即同一个用户也在5月1日之后订购了。
查询返回表order
中的行列表。如果您只需要字段user_id
,则需要添加DISTINCT
以删除重复值:
SELECT DISTINCT user_id
...
答案 6 :(得分:-1)
试试这个
SELECT name FROM my_table WHERE
action_date BETWEEN '2014-03-01' AND '2014-04-30'
HAVING MAX(action_date) <= '2014-04-30'
GROUP BY name