[英]MySQL Find records in one table that has no value of a column in another table
[英]Need the logic to find the value of one column for the min of another column in same table in mysql
我寫了下面的代碼來得到這樣的
SELECT `temp2`.`UserId` AS `UserId`,
`temp2`.`ScheduleDate` AS `ScheduleDate`,
`temp2`.`daystodue`
FROM (
SELECT
`temp1`.`UserId` AS `UserId`,
`temp1`.`ScheduleDate` AS `ScheduleDate`,
`temp1`.`DueDate` AS `DueDate`,
`temp1`.`CompletedState` AS `CompletedState`,
IF(((`temp1`.`CompletedState` = 0) AND ((`temp1`.`ScheduleDate` - CURDATE()) <= 0)), (TO_DAYS(`temp1`.`DueDate`) - TO_DAYS(CURDATE())), 0) AS `daystodue`
FROM (SELECT
`fd_dw`.`ComplianceFactTable`.`UserId` AS `UserId`,
CAST(`fd_dw`.`ComplianceFactTable`.`CourseModule_ScheduleDateID` AS date) AS `ScheduleDate`,
CAST(`fd_dw`.`ComplianceFactTable`.`CourseModule_dueDateID` AS date) AS `DueDate`,
`fd_dw`.`ComplianceFactTable`.`CourseModuleComplete_completionstate` AS `CompletedState`
FROM `fd_dw`.`ComplianceFactTable`
WHERE ((`fd_dw`.`ComplianceFactTable`.`CourseModule_dueDateID` > 0)
AND ((CAST(`fd_dw`.`ComplianceFactTable`.`CourseModule_dueDateID` AS date) - CURDATE()) > 0))) `temp1`) `temp2`
WHERE `temp2`.`UserId` IN (223699,223741,223780,223678,243988,380316,388737,121896,491562)
當我執行上述查詢時,我得到了這個 output
ID Date value
121896 2019-12-06 0
121896 2019-11-06 0
121896 2020-01-06 0
223678 2019-12-23 0
223678 2019-11-23 0
223678 2020-01-23 0
223678 2019-10-23 43
223699 2019-12-23 0
223699 2019-11-23 0
223699 2020-01-23 0
223699 2019-10-23 43
223741 2019-12-23 0
223741 2019-11-23 0
223741 2020-01-23 0
223741 2019-10-23 43
223780 2019-12-23 0
223780 2019-11-23 0
223780 2020-01-23 0
223780 2019-10-23 43
243988 2019-10-15 21
243988 2020-01-15 0
243988 2019-12-15 0
380316 2019-10-05 0
380316 2019-11-05 0
380316 2019-12-05 0
380316 2020-01-05 0
388737 2019-10-23 29
388737 2019-11-23 0
388737 2020-01-23 0
388737 2019-12-23 0
491562 2019-10-17 7
491562 2019-10-17 7
491562 2019-10-17 23
491562 2019-11-17 0
491562 2019-12-17 0
491562 2020-01-17 0
491562 2019-10-17 7
491562 2019-10-17 7
但我想要這樣的東西
121896 2019-11-06 0
223678 2019-10-23 43
223699 2019-10-23 43
223741 2019-10-23 43
223780 2019-10-23 43
243988 2019-10-15 21
380316 2019-10-05 0
388737 2019-10-23 29
491562 2019-10-17 7
我了解,對於每個 ID,您都希望以最短日期記錄。 當有多個具有最短日期的記錄時,您需要具有最短日期的記錄。
在 MySQL 8.0 中,您可以使用 window 函數:
select id, date, value
from (
select
t.*,
row_number() over(partition by id order by date, value) rn
from mytable t
) t
where rn = 1
在早期版本中,您可以使用相關的 suquery 進行過濾:
select id, date, min(value) value
from mytable t
where t.date = (
select t1.date from mytable t1 where t1.id = t.id order by t1.date, t1.value limit 1
)
group by id, date
或者:
select id, date, min(value) value
from mytable t
where t.date = (select min(t1.date) from mytable t1 where t1.id = t.id)
group by id, date
在這個關於 DB Fiddle 的演示中,所有 3 個查詢都返回:
| id | date | value |
| ------ | ---------- | ----- |
| 121896 | 2019-11-06 | 0 |
| 223678 | 2019-10-23 | 43 |
| 223699 | 2019-10-23 | 43 |
| 223741 | 2019-10-23 | 43 |
| 223780 | 2019-10-23 | 43 |
| 243988 | 2019-10-15 | 21 |
| 380316 | 2019-10-05 | 0 |
| 388737 | 2019-10-23 | 29 |
| 491562 | 2019-10-17 | 7 |
Select min(column1), min(column2) from yourtablename
如果您沒有 mysql 8 使用相關查詢
SELECT *
FROM yourTable t1
WHERE date = (SELECT MIN(date)
FROM yourTable t2
WHERE t1.user_id = t2.user_id)
您預期的 output 與您的問題標題不同,因為它包含第一列的每個值,第二列(日期)中具有最小值的行。
使用NOT EXISTS
:
select t.* from tablename t
where not exists (
select 1 from tablename
where id = t.id and date < t.date
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.