简体   繁体   English

MySQL-按日期选择行并传递给php数组

[英]MySQL - Selecting rows by date and passing to php arrays

I have a table with 5 columns which one is date, everytime my event happens at that date, i put a 1 on its column so my tables looks like: 我有一个包含5列的表,其中一个是日期,每次我的事件在该日期发生时,我都会在其列上放置1,因此我的表如下所示:

Data;;;;;;;;;;; 数据;;;;;;;;;;; 0_6 ;;;;;;;;;; 0_6 ;;;;;;;;; 6_12 ;;;;;;;;;; 6_12 ;;;;;;;;;; 12_18 ;;;;;;;;;;; 12_18 ;;;;;;;;;;; 18_24 18_24

2013-02-01     ....1.................0...................1......................0 <br>
2013-02-01    ....0.................0...................1......................0 <br>
2013-02-01    ....0.................1...................1......................0 <br>
2013-02-02    ....0.................0...................1......................0 <br>
2013-02-04    ....1.................0...................0......................0 <br>

So what i want to do is to take the sum of all the columns in the day the event occurs and pass it to a php array. 因此,我想做的是在事件发生的当天获取所有列的总和,并将其传递给php数组。 i have the start date and end date, i was trying that: 我有开始日期和结束日期,我正在尝试:
Where $diferenca = difference in days between end and start day 其中$diferenca = difference in days between end and start day

for($i=0; $i < $diferenca;$i++) {
$query = $con->("SELECT Data, sum(0h_6h) AS sum0_6,sum(6h_12h) AS sum6_12,sum(12h_18h) AS sum12_18,sum(18h_24h) AS sum18_24
FROM mytable
WHERE Data = 'Date_format('DATE(data)+$i','%Y-%M-%e')'
ORDER BY Data ASC
LIMIT 1");

while($row = $query->fetch(PDO::FETCH_ASSOC)....
}

but i think i'm having a problem with the mysql query, could someone help me with this select? 但我认为我在使用mysql查询时遇到问题,有人可以用这个选择帮助我吗? What where clause should i be using to get my result? 我应该使用where子句来获得结果? thanks in advance!! 提前致谢!!

Try : 尝试:

$con->("SELECT 
            Data, 
            sum(0h_6h) AS sum0_6,
            sum(6h_12h) AS sum6_12,
            sum(12h_18h) AS sum12_18,
            sum(18h_24h) AS sum18_24
FROM 
      mytable
WHERE 
      Data BETWEEN start_day AND end_day
GROUP BY 
      Data
ORDER BY
      Data ASC");

change start_day and end_day with acual dates, provided that data is a date type column. 假设数据是日期类型列,请使用开始日期更改开始日期和结束日期。 If not then change the WHERE clause to suit your needs. 如果没有,请更改WHERE子句以适合您的需求。 What you needed, in my opinion, is the GROUP BY part 我认为您需要的是GROUP BY部分

And this is not working? 这不起作用吗?

SELECT Data, sum(`0h_6h`) AS sum0_6,
sum(`6h_12h`) AS sum6_12,
sum(`12h_18h`) AS sum12_18,
sum(`18h_24h`) AS sum18_24
FROM mytable
WHERE Data between '2013-02-01' and '2013-02-04' 
/* Date_format('DATE(data)+$i','%Y-%M-%e') */
GROUP BY data
ORDER BY Data ASC
/*LIMIT 1 */

Notice, I have removed LIMIT 1 clause, added two dates in WHERE clause which you can replace with your "start" date and "end date". 注意,我删除了LIMIT 1子句,在WHERE子句中添加了两个日期,您可以将其替换为“开始”日期和“结束日期”。 Added GROUP BY. 添加了GROUP BY。

If you have 10 days of start date and stop date then your PHP loop will run 10 times which is not a good way to get data out of database. 如果您有10天的开始日期和结束日期,则您的PHP循环将运行10次,这不是从数据库中获取数据的好方法。 Rather, get all data from server in one shot and then process on client side. 而是一站式从服务器获取所有数据,然后在客户端进行处理。

Hope this works for you. 希望这对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM