[英]Filter data with query
id refid date1 date2 nextdate
5 10 2008-02-21 2009-02-21 004/2008
6 10 2009-02-09 2010-02-09 002/2009
7 10 2010-02-08 2011-02-08 001/2010
10 11 2007-02-15 2008-02-15 002/2007
11 11 2008-02-21 2009-02-21 001/2008
12 11 2009-02-09 2010-02-09 001/2009
13 11 2010-02-09 2011-02-09 002/2010
14 11 2011-07-19 2012-07-19 054/2011
15 11 2012-07-17 2014-07-17 066/2012
18 14 2007-02-15 2008-02-15 006/2007
25 16 2007-02-15 2008-02-15 004/2007
27 16 2009-02-10 2010-02-10 004/2009
28 16 2010-02-12 2011-02-12 005/2010
29 16 2011-07-26 2012-07-26 055/2011
30 16 2012-07-18 2014-07-18 067/2012
I have this datatable. 我有这个数据表。 I need query that will do the following: Return all rows for the refid where there exists a date2 value greater than 2014-01-01 (If there is id then I need all ids)
我需要执行以下操作的查询:返回存在大于2014-01-01的date2值的refid的所有行(如果有id则我需要所有id)
Result should be like this: 结果应该是这样的:
id refid date1 date2 nextdate
10 11 2007-02-15 2008-02-15 002/2007
11 11 2008-02-21 2009-02-21 001/2008
12 11 2009-02-09 2010-02-09 001/2009
13 11 2010-02-09 2011-02-09 002/2010
14 11 2011-07-19 2012-07-19 054/2011
15 11 2012-07-17 2014-07-17 066/2012
25 16 2007-02-15 2008-02-15 004/2007
27 16 2009-02-10 2010-02-10 004/2009
28 16 2010-02-12 2011-02-12 005/2010
29 16 2011-07-26 2012-07-26 055/2011
30 16 2012-07-18 2014-07-18 067/2012
This is quite basic but if I've understood you correctly: 这是很基本的,但是如果我正确理解了你的话:
DELETE FROM tableName
WHERE date2 < '2014-01-01' AND NOT (refid IS NULL)
Edit 编辑
As stated in the comment below I possibly misunderstood the following part of the question to mean that the entries needed deleting. 如下面的评论所述,我可能会误解问题的以下部分,以表示需要删除条目。
Remove all rows with refid where date2 is less than 2014-01-01
删除date2小于2014-01-01的所有带有refid的行
In which case the following would return all results after the specified date instead: 在这种情况下,以下内容将返回指定日期之后的所有结果:
SELECT *
FROM tableName
WHERE date2 > '2014-01-01' AND NOT (refid IS NULL)
I have also assumed that the "with refid" means that refid
should be populated. 我还假设“ with refid”表示应填充
refid
。
To get the results described in the original question however I'd simplify the query as follows as the date part specificied wouldn't give the results: 为了获得原始问题中描述的结果,但是我将查询简化如下,因为指定日期的部分不会给出结果:
SELECT *
FROM tableName
WHERE (refid = 11) OR (refid = 16)
Another Edit 另一个编辑
The following should return all entries assigned to a refid
with any entries with a date2
after 2014-01-01
以下应返回分配给所有条目
refid
与任何条目与date2
后2014-01-01
SELECT DISTINCT t1.*
FROM tableName t1
INNER JOIN tableName t2 ON t1.refid = t2.refid AND t2.date2 > '2014-01-01'
Alternatively you could use exists: 或者,您可以使用存在:
SELECT *
FROM tableName t1
WHERE EXISTS(
SELECT *
FROM tableName t2
WHERE t1.refid = t2.refid AND t2.date2 > '2014-01-01'
)
Or link to a separate table of refid
's which fit the profile 或链接到适合个人资料的
refid
的单独表
SELECT *
FROM tableName t1
INNER JOIN (
SELECT DISTINCT refid
FROM tableName
WHERE date2 > '2014-01-01'
)t2 ON t1.refid = t2.refid
This might do what you want, if I understand your question correctly: 如果我正确理解您的问题,这可能会做您想要的事情:
SELECT *
FROM datatable
WHERE refid IN (SELECT DISTINCT refid
FROM datatable
WHERE date2 > '20140101')
The subquery gives back all distinct refid values for which a date2 exists that is greater than 2014-01-01. 子查询返回存在的date2大于2014-01-01的所有不同的refid值。 You use the list of these refid 's to return all rows with those refid 's.
您可以使用这些refid的列表来返回所有包含这些refid的行。
Edit 编辑
Adding an SQL Fiddle to experiment with. 添加一个SQL Fiddle进行试验。 (Also, SQL Fiddle is a very useful tool to help you formulate your database questions clearer & make them more likely to be answered in a useful way!)
(此外, SQL Fiddle是一个非常有用的工具,可以帮助您更清楚地表达数据库问题,并更有可能以一种有用的方式回答它们!)
To get the results described in your question the following works. 为了获得问题中描述的结果,以下工作。
SELECT
*
FROM
datatable
WHERE
refid = 11
OR refid = 16
If you look at your required results: 如果您查看所需的结果:
id refid date1 date2 nextdate
10 11 2007-02-15 2008-02-15 002/2007
11 11 2008-02-21 2009-02-21 001/2008
12 11 2009-02-09 2010-02-09 001/2009
13 11 2010-02-09 2011-02-09 002/2010
14 11 2011-07-19 2012-07-19 054/2011
15 11 2012-07-17 2014-07-17 066/2012
25 16 2007-02-15 2008-02-15 004/2007
27 16 2009-02-10 2010-02-10 004/2009
28 16 2010-02-12 2011-02-12 005/2010
29 16 2011-07-26 2012-07-26 055/2011
30 16 2012-07-18 2014-07-18 067/2012
You will notice that the date2
field does not meet the criteria which you have defined in your question: 您会注意到
date2
字段不符合您在问题中定义的条件:
I have this datatable.
我有这个数据表。 I need to filter datatable by following condition: Remove all rows with
refid
wheredate2
is less than 2014-01-01我需要通过以下条件过滤数据表:删除
date2
小于2014-01-01的所有带有refid
行
The date2 fields range from between 2008-02-15 and 2014-07-18. date2字段的范围为2008-02-15至2014-07-18。 The only other reference which you have stated in your criteria was "with
refid
". 您在条件中指出的唯一其他参考是“ with
refid
”。 Looking at your table and the results required, you will notice that the refid
of the items you wanted to select were either 11 or 16 so the filtering on date specified in the question is proving to be more of a distraction to those attempting to help. 查看表和所需的结果,您会发现想要选择的项目的
refid
是11或16,因此事实证明,对问题中指定的日期进行过滤会对那些试图提供帮助的人造成更多的干扰。 You should consider clarifying your question to prevent further confusion and maybe creating an sql fiddle (goto: http://sqlfiddle.com/ ) so that people have the ability to compare their results to your required results rather than telling people to re-read your question. 您应该考虑澄清您的问题,以防止进一步的混乱,并可能创建一个sql小提琴(goto: http : //sqlfiddle.com/ ),以便人们能够将其结果与所需结果进行比较,而不是告诉人们重新阅读。你的问题。
EDIT: 编辑:
The following should return all dataTable results where there is one or more dataTable entries with the same refid
and date2 > '2014-01-01'
如果存在一个或多个具有相同
refid
和date2 > '2014-01-01'
dataTable条目,则以下应返回所有dataTable结果
SELECT *
FROM datatable t1
INNER JOIN (
SELECT DISTINCT refid
FROM datatable
WHERE date2 > '2014-01-01'
)t2 ON t1.refid = t2.refid
尝试这个
SELECT * FROM table WHERE date2 >='2014-01-01'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.