PHP MySQL, sum of rows based on datetime row - php

I'm trying to sum number of persons based on datetime rows.
this is how my table looks like:
id | date | person |
---+-------------------+--------+
1 |2013-12-26 00:00:00| 3 |
---+-------------------+--------+
2 |2013-12-26 00:00:00| 2 |
---+-------------------+--------+
3 |2013-12-26 00:00:00| 3 |
---+-------------------+--------+
4 |2018-10-21 00:00:00| 3 |
---+-------------------+--------+
What i want my query to do is: Sum all persons based on date. But i think my problem is not query it self (or maybe it is), but the datetime. I have something like
$date = "26.12.2013";
$date = strtotime(date("d.m.Y", strtotime($date)));
$date = date("Y-m-d", $date);
$query= "SELECT '$date', SUM('person') totalperson FROM table_name WHERE date='$date' GROUP BY '$date'";
but the number returned is 2016 and expected is 8 :)
I hope my question is clear enough.

SUM(`person`)
Use backticks for column name,you are suming a string

What db engine do you use?
Anyway, this should work:
$date = "2013-12-26 00:00:00";
$query = "
SELECT date, SUM(person) totalperson
FROM table_name WHERE date='$date'
GROUP BY date
";

SELECT
date,
SUM(person) totalperson
FROM table_name
WHERE DATE(date) = '$date'
GROUP BY date
Use DATE() function
you are providing date string in date time column

Related

Selecting entries by date and by time seperately with MySQL

I have this query that selects every appointment with status Pending. Works well. The problem with this query it will also select appointments that are pending in the past. I only want to display those that are either today at a later hour than current_time or simply at a later date. Time and date are in a different column. In the example below only the second and third row should be returned. I'm giving you the full query as it is used and working in my app right now. How can this be achieved?
user_schedule table
id | customer_id | date | time | cleaning_status
1 | 345 | 2020-06-09 | 08:00:00 | Pending
2 | 768 | 2020-06-09 | 19:00:00 | Pending
3 | 913 | 2020-06-11 | 07:00:00 | Pending
PHP
if(!empty($_POST)){
//variables
$current_time ='16:00:00';
$current_date ='2020-06-09';
$my_city ='Miami';
$sstatus_o = 'Pending';
//query
$data = $conn->prepare("select *,us.id as orderid,us.customer_id
as ownerId from user_schedule us
left join users u
on us.customer_id=u.id
LEFT JOIN user_avatar ua
ON us.customer_id=ua.user_id
and ua.last_update = (select
max(last_update)
from user_avatar ua1 where
ua.user_id=ua1.user_id)
left join user_address uad
on us.customer_id=uad.user_id
where (uad.city LIKE ?) AND
us.cleaning_status=? ORDER BY us.id DESC");
$data->bind_param('ss',$my_city,$sstatus_o);
$data->execute();
$result_data = $data->get_result();
}
You can add another constraint to your query that checks whether the timestamp formed from your date and time values is greater than your $current_date and $current_time values i.e.
WHERE uad.city LIKE ?
AND us.cleaning_status = ?
AND TIMESTAMP(us.date, us.time) > TIMESTAMP(?, ?)
and then add the $current_date and $current_time variables to the bind_param i.e.
$data->bind_param('ssss', $my_city, $sstatus_o, $current_date, $current_time);

query db for date time more than today

i have a table where i am storing my expiry date like thi:
| id | expire_date |
| 2 | 2019-09-19T15:34 |
and i want to query the table with todays date and time and onwards so far this is what i got;
date_default_timezone_set("Pacific/Fiji");
$now = date('Y-m-d\TH:i');
"SELECT * FROM `table` WHERE $now > `expire_date`"
but this does not work .
the is not a sql injection problem.
You need to add column name after the WHERE clause not after greater than symbol >
"SELECT * FROM table WHERE expire_date >= '$now'"
For the reference you can visit here

MySql get records that are more than 7 days old (Varchar)

I have a column in my database called end_date which is a varchar and not a date, the format is 01/07/2015. I am trying to pull rows that are more than 7 days old from today's date.
I have tried the following.
$d = date('d/m/Y', strtotime(' -7 day')); // 7 days ago 24/06/2015
$sql = mysql_query("SELECT * FROM products WHERE end_date < '".$d."'");
or
$sql = mysql_query("SELECT * FROM products WHERE DATE_FORMAT(STR_TO_DATE(end_date, '%d/%m/%Y'), '%Y%m%d') < '".$d."'");
Which all return rows from yeserday, 2 days ago etc rather than only rows that are more than 7 days old. Is there a way to do this if the end_date column is varchar?
In your where clause try to put this:
WHERE str_to_date(end_date, '%d/%m/%Y') <= DATE_SUB(SYSDATE(), INTERVAL 7 DAY)
The varchar dates are not real date once you do comparison it should always be in the form of Y-m-d format. Here what happens when you use str_to_date
mysql> select str_to_date('01/07/2015','%d/%m/%Y') as d ;
+------------+
| d |
+------------+
| 2015-07-01 |
+------------+
The returned date is in Y-m-d format and you can use mysql functions for the comparison such as date_sub()
select * from products
where
str_to_date(end_date, '%d/%m/%Y') < date_sub(curdate(),interval 7 day);
Here how date_sub works
mysql> select date_sub(curdate(),interval 7 day) as old_day;
+------------+
| old_day |
+------------+
| 2015-06-24 |
+------------+
1 row in set (0.00 sec)

Select dates between this monday and this friday

I have a table with a date field (2013-07-11 = July 11th, 2013).
I need to select entries from the monday (the start of my week) of any given week until the end of that week.
I've seen queries that can grab rows whose date is 1 week ago, but I specifically need to grab rows that have a date field that occurs this week, regardless of what the current day of the week is.
I'm currently grabbing it in php using this, but I feel like there has to be a mysql method for calculating the beginning and end of this week:
$this_monday = date('Y-m-d', strtotime('previous monday'));
$this_friday = date('Y-m-d',strtotime('this friday'));
$sql = "SELECT `date`, $sign FROM `horoscopes`
WHERE `date` >= '$this_monday' AND `date` <= '$this_friday'
AND type = 'Daily'
ORDER BY `date` ASC
";
And I'm pretty sure that on monday it will end up grabbing monday of last week, not monday.
from this data set I'd like to select the following:
+------------+
| date |
+------------+
| 2013-07-12 |
| 2013-07-11 |
| 2013-07-10 |
| 2013-07-09 |
| 2013-07-17 |
| 2013-07-08 |
| 2013-07-05 |
+------------+
7-8, 7-9, 7-10, 7-11, 7-12 regardless of the day of the week.
Just learned about the WEEK() function in mysql
SELECT `date` FROM horoscopes WHERE WEEK(`date`) = WEEK(NOW(), -1);

mysql select month from timestamp

I have a mysql query but it's not working properly yet. The idea is: I have a month calendar, so I select all items with the current month form my db.
I select TV show episodes, they air on a specific date but they are available the next day. I want to have the available dates in my cal. So a specific example: An episode aires 2013-03-31 and is available on 2013-04-01. This episode will not be selected from the db. I have tried something like this:
$this->db->where('MONTH(FROM_UNIXTIME(first_aired+86400))='.$month);
But it didn't work. Does anyone know a solution? This is my full code:
$this->db->select('*, episodes.overview as overview, episodes.id as ep_id, shows.id as id');
$this->db->from('episodes');
$this->db->join('shows', 'episodes.imdb_id = shows.imdb_id');
$this->db->where('CHAR_LENGTH(episodes.ep_title) > 1');
$this->db->where('MONTH(FROM_UNIXTIME(first_aired))='.$month);
$this->db->where('YEAR(FROM_UNIXTIME(first_aired))='.$year);
$this->db->order_by('episodes.first_aired', 'DESC');
$data = $this->db->get()->result_array();
Thanks in advance!
Use MySQLs DATE_ADD function:
select mydate, date_add(mydate, interval 1 day) from testdata;
So if you want to get the month of the next day, use month(date_add(...))
mysql> select mydate, date_add(mydate, interval 1 day) "next day", month(date_add(mydate, interval 1 day)) "month of next day" from testdata;
+------------+------------+-------------------+
| mydate | next day | month of next day |
+------------+------------+-------------------+
| 2013-01-31 | 2013-02-01 | 2 |
| 2013-01-01 | 2013-01-02 | 1 |
+------------+------------+-------------------+
2 rows in set (0.00 sec)
So for your query, use something like $this->db->where('MONTH(DATE_ADD(first_aired, INTERVAL 1 DAY))='.$month);. Oh, and you might have to add a FROM_UNIXTIME here and there.

Categories