我有一个名为“ Project”的表,其中包含以下项目数据。
+--------+------------+------------+----------+--------+---------------+--------+
| In Num | pro_no_old | pro_no_new | pro_name | amount | budget_status | status |
+--------+------------+------------+----------+--------+---------------+--------+
| 1 | pro1 | | project1 | 200000 | original | 1 |
| 2 | pro2 | | project2 | 150000 | original | 1 |
| 3 | pro3 | | project3 | 300000 | original | 1 |
| 4 | pro2 | pro4 | project4 | 100000 | revised | 0 |
+--------+------------+------------+----------+--------+---------------+--------+
02)pro_name“ Project4”已通过修改从Project2变为。然后,我需要获得没有修订项目的项目,例如Project2。换句话说,无需获得先前项目的修订版。期望的投放可能如下。
+--------+------------+------------+----------+--------+---------------+--------+
| In Num | pro_no_old | pro_no_new | pro_name | amount | budget_status | status |
+--------+------------+------------+----------+--------+---------------+--------+
| 1 | pro1 | | project1 | 200000 | original | 1 |
| 3 | pro3 | | project3 | 300000 | original | 1 |
| 4 | pro2 | pro4 | project4 | 100000 | revised | 0 |
+--------+------------+------------+----------+--------+---------------+--------+
03)我使用以下查询来获得所需的输出。
SELECT * FROM project WHERE('pro_no_old in (SELECT pro_no_old FROM project where budget_status = "original"))
04)但是没有得到预期的结果。谁能帮我 ?
+--------+------------+------------+----------+--------+---------------+--------+
| In Num | pro_no_old | pro_no_new | pro_name | amount | budget_status | status |
+--------+------------+------------+----------+--------+---------------+--------+
| 1 | pro1 | | project1 | 200000 | original | 1 |
| 3 | pro3 | | project3 | 300000 | original | 1 |
| 5 | pro4 | pro5 | project5 | 50000 | revised | 0 |
+--------+------------+------------+----------+--------+---------------+--------+
答案 0 :(得分:0)
我不确定您的数据模型,但是如果您只对pro_no_old的最后一个条目感兴趣
select *
from t
where innum = (select max(innum) from t t1 where t1.pro_no_old = t.pro_no_old);
+-------+------------+------------+----------+--------+---------------+--------+
| InNum | pro_no_old | pro_no_new | pro_name | amount | budget_status | status |
+-------+------------+------------+----------+--------+---------------+--------+
| 1 | pro1 | NULL | project1 | 200000 | original | 1 |
| 3 | pro3 | NULL | project3 | 300000 | original | 1 |
| 4 | pro2 | pro4 | project4 | 100000 | revised | 0 |
+-------+------------+------------+----------+--------+---------------+--------+
3 rows in set (0.00 sec)