gpt4 book ai didi

mysql - 如何从表中获取基于月份和年份的记录?

转载 作者:行者123 更新时间:2023-11-29 02:40:00 24 4
gpt4 key购买 nike

我们可以在 where 子句中仅使用日期的月份和年份部分来过滤日期字段吗?

    SELECT `outlet`.`outlet`,cities.`city`,teritory.`teritory`,region.`region`,`countries`.`country` FROM `audit`
JOIN `user` ON `user`.`id`=`audit`.`auditor_id`
JOIN `user_outlet` ON `user_outlet`.id=`audit`.`outlet_id`
JOIN `outlet` ON `outlet`.id=`user_outlet`.`outlet_id`
JOIN `cities` ON `cities`.`id`=`outlet`.`city_id`
JOIN `teritory_city` ON `teritory_city`.`city_id`=`cities`.id
JOIN `teritory` ON `teritory_city`.`teritory_id`=`teritory`.`id`
JOIN `region` ON `region`.`id`=`teritory`.`region_id`
JOIN `org_country` ON `org_country`.`id`=`region`.`org_country_id`
JOIN `countries` ON `countries`.`id`=`org_country`.`country_id`
WHERE `audit`.`created_date`=?

上面是我试过的查询,我必须传递年和月参数而不是传递日期。在 UI 中,年和月是我提供给用户的唯一过滤器,用于获取特定月份和年份的相应数据。

最佳答案

使用 year()month() 函数,如下所示-

SELECT `outlet`.`outlet`,cities.`city`,teritory.`teritory`,region.`region`,`countries`.`country` FROM `audit`
JOIN `user` ON `user`.`id`=`audit`.`auditor_id`
JOIN `user_outlet` ON `user_outlet`.id=`audit`.`outlet_id`
JOIN `outlet` ON `outlet`.id=`user_outlet`.`outlet_id`
JOIN `cities` ON `cities`.`id`=`outlet`.`city_id`
JOIN `teritory_city` ON `teritory_city`.`city_id`=`cities`.id
JOIN `teritory` ON `teritory_city`.`teritory_id`=`teritory`.`id`
JOIN `region` ON `region`.`id`=`teritory`.`region_id`
JOIN `org_country` ON `org_country`.`id`=`region`.`org_country_id`
JOIN `countries` ON `countries`.`id`=`org_country`.`country_id`
WHERE year(`audit`.`created_date`)=yearparam and month(`audit`.`created_date`)=monthparam

关于mysql - 如何从表中获取基于月份和年份的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759160/

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