gpt4 book ai didi

php - 使用php/SQL从数据库查询信息

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

database columns

我想使用这两列从表中选择数据。我想要做的是选择月份二月和八月之间以及年份2003年和2005年之间的表格em> 包括 2004 年和 2005 年的一月。我已经尝试过:`

$number = range(3, 8);  

foreach($number as $key=> $value):
$where .= " Month ='$value' AND Year BETWEEN '2003' AND '2005' or " ;
endforeach;
if(strlen(trim($where)) > 0)

$where = substr(trim($where), 0, -2);`
$query = "SELECT * FROM myTable $where";

返回以下查询

SELECT * FROM myTable WHERE Month ='2' AND Year BETWEEN '2003' AND '2005' or Month ='3' AND Year BETWEEN '2003' AND '2005' or Month ='4' AND Year BETWEEN '2003' AND '2005' or Month ='5' AND Year BETWEEN '2003' AND '2005' or Month ='6' AND Year BETWEEN '2003' AND '2005' or Month ='7' AND Year BETWEEN '2003' AND '2005' or Month ='8' AND Year BETWEEN '2003' AND '2005'

但是我的查询的问题是它没有选择 2004 年和 2005 年 1 月。有人知道如何做到这一点吗?

最佳答案

SELECT * FROM myTable 
WHERE ( Month >=2 AND Month <=8 AND year BETWEEN 2003 AND 2005) OR (Month=1 AND (year=2004 OR year=2005))

关于php - 使用php/SQL从数据库查询信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38701343/

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