我有一个名为data_tbl
的mysql表,其中包含不同ID的行,如下所示
-----------+------------+----------+---------------------+-------------+
id | date | user | field | change_from | change_to
-----------+------------+----------+---------------------+-------------+
7293 | 08/29/2017 | chey | Status | M | R
7293 | 08/29/2017 | chey | File Name | | Diffs-com
7293 | 08/29/2017 | chey | Status | V | M
7293 | 08/07/2017 | chey | Note Title | | static-an
2293 | 08/07/2017 | chey | File Name | | Diffs-com
7293 | 06/28/2017 | dets | Is-customer-visible | N | Y
7293 | 05/30/2017 | unmx | Status | R | V
2293 | 05/24/2017 | chey | Bug-origin | | Code / Im
3593 | 05/24/2017 | chey | Regression | | N
7293 | 05/24/2017 | chey | Status | A | R
3293 | 05/24/2017 | chey | Note Title | | static-an
4293 | 05/24/2017 | chey | File Name | | Diffs-com
4293 | 05/24/2017 | chey | File Name | | Diffs-com
5293 | 05/22/2017 | chey | Note Title | | static-an
7293 | 05/22/2017 | chey | File Name | | Diffs-com
7293 | 04/28/2017 | gogi | Engineer | gogi | chey
7293 | 04/25/2017 | dets | Is-customer-visible | | N
7293 | 04/25/2017 | itka | Assigner | | itka
7293 | 04/25/2017 | itka | Engineer | | gogi
7293 | 04/25/2017 | itka | Status | N | A
7093 | 04/25/2017 | koga | Defect Created | |
7393 | 04/25/2017 | vkal | Defect Created | |
7293 | 04/25/2017 | vkal | Defect Created | |
7293 | 04/25/2017 | vkal | Defect Created | |
7293 | 04/25/2017 | vkal | Defect Created | |
-----------+------------+----------+---------------------+-------------+
从上表中我如何编写一个自我过滤的查询,如下所示
select * from data_tbl where id in ('7293', '7093') and
(from the filtered rows from above, filter when field is "engineer" change_to must be "chey" and when field is status change_from should be "A" and change_to should be "R")
我的目标是从上面过滤数据集,得到类似下面的内容
-----------+------------+----------+---------------------+-------------+
id | date | user | field | change_from | change_to
-----------+------------+----------+---------------------+-------------+
7293 | 05/24/2017 | chey | Status | A | R
7293 | 04/28/2017 | gogi | Engineer | gogi | chey
-----------+------------+----------+---------------------+-------------+
答案 0 :(得分:1)
你的病情
- 当字段是“工程师”时,change_to必须是“chey”
- 当字段为status时,change_from应为“A”,change_to应为“R”
醇>
您可以使用UNION
结合两个选择查询。
SELECT ID,DATE,USER,field,change_from,change_to
FROM
(
SELECT ID,DATE,USER,field,change_from,change_to
FROM data_tbl
WHERE change_from = 'A' AND change_to = 'R'
UNION
SELECT ID,DATE,USER,field,change_from,change_to
FROM data_tbl
WHERE field = 'engineer' AND change_to = 'chey'
) AS A
WHERE A.ID in ('7293', '7093')
答案 1 :(得分:1)
使用此作为描述
where id in ('7293', '7093') AND
- ...
when field is "engineer" ... change_to must be "chey" OR ...
- ...
醇>when field is status ... change_from should be "A" and change_to should be "R")
SQL成为:
select * from data_tbl where id in ('7293', '7093')
AND (
(field = 'Engineer' AND change_to = 'chey') OR
(field = 'Status' AND change_from = 'A' AND change_to = 'R')
);
答案 2 :(得分:1)
根据您的要求和所需结果,您只需要select
语句,其中包含一些过滤器和case
语句,以生成所需数据:
SELECT id, date, user, field,
CASE WHEN field = 'Status' THEN 'A'
ELSE change_from
END change_from
CASE WHEN field = 'Engineer' THEN 'chey'
WHEN field = 'Status' THEN 'R'
ELSE change_to
END change_to
WHERE id IN ('7293', '7093')
AND field IN ('Engineer', 'Status');
这将与您根据需要添加的结果完全相同。当然基于提供的样本数据。
答案 3 :(得分:1)
这些只是过滤器,除了当你说“当......和(而不是)那个......”时,“和”实际上是一个逻辑OR,因为你想要两个条件都可以接受。 / p>
过滤字段为“工程师”时,change_to必须为“chey”
((`field` = 'Engineer' AND change_to = 'chey')
当字段为状态时,change_from应为“A”,change_to应为“R”
(`field` = 'Status' AND change_from = 'A' AND change_to = 'R'))
所以:
WHERE id IN (7093, 7293)
AND
(
(`field` = 'Engineer' AND change_to = 'chey')
OR
(`field` = 'Status' AND change_from = 'A' AND change_to = 'R')
)
如果你想拥有那些状态和工程师系列,还有其他人(例如回归,分配者......),如果这些是最后几行,你可以明确命名:
...
OR
(`field` = 'Status' AND change_from = 'A' AND change_to = 'R')
OR
(`field` = 'Assigner')
OR
(`field` = 'Note Taken')
另一方面,如果有很多类型,通过指定“其他”可以更好地服务:带有既不是Statud也不是工程师的字段的记录。
...
OR
(`field` = 'Status' AND change_from = 'A' AND change_to = 'R')
OR
(`field` NOT IN ('Status', 'Engineer'))