gpt4 book ai didi

mysql - where 条件失败时用户定义的 Mysql 函数

转载 作者:行者123 更新时间:2023-11-29 16:04:43 25 4
gpt4 key购买 nike

我创建了 Mysql 函数,它给出结果整数或 NULL。它适用于所有值。如果在简单查询中使用此函数,它也会按预期工作,如以下查询:

select distinct(hsd.hotel_id) 
from hotel_season_detail hsd
where '2019-04-26' BETWEEN hsd.season_fromdate and hsd.season_todate and hsd.stop_sale >'2019-04-26' and getHotelMarkupManagementId(hsd.id,'AFG') is NOT NULL

getHotelMarkupManagementId()是用户定义的mysql函数。如上面的查询返回两个酒店 ID,这两个酒店 ID 已存在于酒店表中。

但是当这个查询添加为子查询时

select * from hotel_service 
where id in
(select distinct(hsd.hotel_id)
from hotel_season_detail hsd where '2019-04-26' BETWEEN hsd.season_fromdate and hsd.season_todate and hsd.stop_sale >'2019-04-26' and getHotelMarkupManagementId(hsd.id,'AFG')
is NOT NULL)

它给出了错误的结果。它没有按预期工作。select * from hotel_service where id in (125,126) 给出结果,如果单独运行,子查询还会给出 id 125 和 126 作为结果。但是当添加为子查询时它会失败

此行为是否应归因于 IN 子句中的 mysql 函数?请提出建议。

最佳答案

我认为这是 Mysql 中使用函数时的一个问题,因为如果相同的第二个查询条件分为两个子查询,那么我的问题就得到了解决。

select * from hotel_service where id in (select distinct(hsd.hotel_id) from hotel_season_detail hsd where '2019-04-26' BETWEEN hsd.season_fromdate and hsd.season_todate and hsd.stop_sale >'2019-04-26') and id in (select distinct(hsd.hotel_id) from hotel_season_detail hsd where getHotelMarkupManagementId(hsd.id,'AFG') is not NULL)

我在不同的子查询中分离了 MySQL 函数,但组合相同的条件,然后没有给出正确的结果。所以我建议使用与其他条件不同的mysql功能,如果条件是BETWEEN OR >运算符。

关于mysql - where 条件失败时用户定义的 Mysql 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55849954/

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