gpt4 book ai didi

mysql - php sql查询在月份和年份之间选择

转载 作者:行者123 更新时间:2023-11-29 20:26:58 25 4
gpt4 key购买 nike

我想在月份和年份之间的where条件中选择SQL查询。在我的数据库中,有字段名称月份和年份,用于商店生产月份和年份的输入。现在我想要搜索产生2015年12月至2016年8月的输入这是我的sql

SELECT * FROM tbl_produce p WHERE p.status =0 AND ((p.months >='12' 
AND p.years>='2015') AND (p.months <='8' AND p.years<='2016)) ;

但结果返回NULL,我知道我的查询不正确,但如何纠正它?

最佳答案

为什么不将日期列设计为日期?

如果您想保留当前的设计,请尝试以下操作:

SELECT * 
FROM tbl_produce
WHERE status = 0
AND concat(years, if(months > 9, months, concat('0', months)))
BETWEEN '201512' AND '201608'

或者

SELECT * 
FROM tbl_produce
WHERE status = 0
AND (
(years = 2015 AND months = 12)
OR
(years = 2016 AND months < 9)
)

关于mysql - php sql查询在月份和年份之间选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39262253/

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