gpt4 book ai didi

mysql - 在一列中选择具有多个选项的单个值

转载 作者:行者123 更新时间:2023-11-29 15:26:30 24 4
gpt4 key购买 nike

当两个行号值按顺序相同时,我需要根据日期选择一个值。

这里有一个 sqlfiddle 只是为了准确显示我的表的结构。 http://sqlfiddle.com/#!9/2ccf800/1

这是我尝试过的:

SELECT DISTINCT `Line Number`
, `Contract Number`
, `Date Promised`
, `roytecp_n`
FROM otis_pos
RIGHT
JOIN `otis rev link`
on otis_pos.`Item Number` = `otis rev link`.otis_pn
WHERE otis_pos.`Contract Number` <> ''
AND (
(`Date Promised` <= CURRENT_DATE() + INTERVAL 28 DAY
AND (roytecp_n Like 'OTH%'
OR roytecp_n Like '9O%'
OR roytecp_n Like '81%'
OR roytecp_n is NULL
)
OR
( `Date Promised` > CURRENT_DATE() + INTERVAL 28 DAY
AND (roytecp_n Like '88%'
OR roytecp_n Like '89%'
OR roytecp_n IS NULL
)
)
)

这将返回两组值,而不是指定的值之一。

这是我的数据:

Line Number    Contract Number    Date Promised    roytecp_n
1 648756 2020-01-24
2 648756 2020-01-24 OTHW600HZ974
2 648756 2020-01-24 89OTHW600HZ974
3 648756 2020-01-24 OTHW600HY998
3 648756 2020-01-24 89OTHW600HY998
1 648756 2020-01-24
2 648756 2020-01-24 OTHW600DV998
2 648756 2020-01-24 89OTHW600DV998
3 648756 2020-01-24 9O04174AKV96-B
3 648756 2020-01-24 OTHWC00DT998

以下是当值大于或等于 current_date + 28 天时需要返回数据的方式:

Line Number    Contract Number    Date Promised    roytecp_n
1 648756 2020-01-24
2 648756 2020-01-24 89OTHW600HZ974
3 648756 2020-01-24 89OTHW600HY998
1 648756 2020-01-24
2 648756 2020-01-24 89OTHW600DV998
3 648756 2020-01-24 9O04174AKV96-B
4 648756 2020-01-24 OTHWC00DT998

以下是当值小于或等于 current_date + 28 天时需要返回数据的方式

Line Number    Contract Number    Date Promised    roytecp_n
1 648756 2020-01-24
2 648756 2020-01-24 OTHW600HZ974
3 648756 2020-01-24 OTHW600HY998
1
1 648756 2020-01-24
2 648756 2020-01-24 OTHW600DV998
3 648756 2020-01-24 9O04174AKV96-B
4 648756 2020-01-24 OTHWC00DT998

最佳答案

似乎你错了(在第一个和

  SELECT DISTINCT `Line Number`
, `Contract Number`
, `Date Promised`
, `roytecp_n`
FROM otis_pos
RIGHT JOIN `otis rev link`
on otis_pos.`Item Number` = `otis rev link`.otis_pn
WHERE otis_pos.`Contract Number` <> ''
AND (`Date Promised` <= CURRENT_DATE() + INTERVAL 28 DAY
AND (roytecp_n Like 'OTH%'
OR roytecp_n Like '9O%'
OR roytecp_n Like '81%'
OR roytecp_n is NULL
)
OR
( `Date Promised` > CURRENT_DATE() + INTERVAL 28 DAY
AND (roytecp_n Like '88%'
OR roytecp_n Like '89%'
OR roytecp_n IS NULL
)
)
)

http://sqlfiddle.com/#!9/2ccf800/3

关于mysql - 在一列中选择具有多个选项的单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59039122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com