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
Related
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);
I have some a MYSQL db with a DATETIME field that updates itself every minute while a user is logged in via AJAX. This works perfectly. However I am trying to get a count of how many logged in users there are and it is not working
$table = 'mytable';
$query = "SELECT * FROM $table WHERE lidtm > DATE_SUB(DATE(now()), INTERVAL 1 MINUTE)";
$result = mysqli_query($dbc, $query);
$num = mysqli_num_rows($result);
echo $num;
theoretically this should tell me how many users are logged in, because each users 'lidtm' field updates every minute while they are logged in, so if the DATE_SUB function counts the rows in which the lidtm field is > that now() - 1 minute, it should tell me all users that have been updated by my ajax within the last minute.
but for some reason it always returns all rows.
what am I missing. I need a count of how many rows the 'lidtm' field is within the last minute.
Wrapping now() with date() casts it to a date, i.e., without a time:
select date(now());
+-------------+
| date(now()) |
+-------------+
| 2017-05-16 |
+-------------
If you want to measure minutes, you need the time:
select now();
+---------------------+
| now() |
+---------------------+
| 2017-05-16 13:56:00 |
+---------------------+
So just take out the call to date(), and use:
select date_sub(now(), interval 1 minute);
+------------------------------------+
| date_sub(now(), interval 1 minute) |
+------------------------------------+
| 2017-05-16 13:56:17 |
+------------------------------------+
Can anyone tell me how to delete items from a table with more than 5 days in CodeIgniter please? I searched here and on google and can't find anything that works.
EDIT:
My table in database looks like this:
| ID| Name | CreationDate |
| 1 | Test | 01-12-2014 |
| 2 | Test2| 01-11-2014 |
and so on, I hope you understand me.
Steps:
Get the current date
Calculate the date 5 days ago
Delete the items from the database where the CreationDate is less than (older than) 5 days
Code:
$date = date("Y-m-d H:i:s",time());
$date = strtotime($date);
$min_date = strtotime("-5 day", $date);
$this->db->where("CreationDate < '$min_date'", NULL, FALSE);
What is the type of CreationDate? What do you mean by more than 5 days?
If you mean you want to delete items where CreationDate is older than 5 days, and CreationDate type is string (varchar), you could use this:
$this->db->query("DELETE FROM MyTable where STR_TO_DATE(CreationDate, '%d-%m-%Y') < DATE_SUB(NOW(), INTERVAL 5 DAY)";
But if CreationDate type is mysql date, then simply use:
$this->db->query("DELETE FROM MyTable where CreationDate < DATE_SUB(NOW(), INTERVAL 5 DAY)";
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
I have a table that stores creation and closure dates. What I'm trying to do is run a query that gives me in a given time period, for each day, how many records were created, how many were closed, and how many were open. Table structure:
+------------------------+
| Field | Structure |
+------------+-----------+
| id | int |
| start_date | datetime |
| close_date | datetime |
+------------+-----------+
The first two queries are not a problem - what I'm working with right now is this:
SELECT COUNT(*), start_date FROM dates
WHERE start_date BETWEEN 'start' AND 'end'
GROUP BY start_date
ORDER BY start_date ASC
SELECT COUNT(*), close_date FROM dates
WHERE close_date BETWEEN 'start' AND 'end'
GROUP BY close_date
ORDER BY close_date ASC
The third query is an issue - I can get it to give me how many were open on a day where that day exists in the table, but what I haven't figured out is how to make it write out a row for every day in the year and give me the count for that day.
That's the first problem.
The second is that I want to get it to give me a result like this, and I can't figure out how to do it - searching around here has given me a few false starts, but nothing solid.
+------------+--------+--------+--------+
| Date | Opened | Closed | Active |
+------------+--------+--------+--------+
| 2012-05-06 | 3 | 2 | 7 |
| 2012-05-07 | 2 | 0 | 9 |
| 2012-05-08 | 0 | 0 | 9 |
| 2012-05-09 | 0 | 3 | 6 |
+------------+--------+--------+--------+
I'm entirely willing to run separate queries if necessary and handle all of it in code instead of db if I have to - I'm fairly sure I can write all of the query data from the individual queries to a single array and just loop through that to build the report, but I'd really prefer not. Any suggestions?
This query is tricky because you want the status list for every date in range.
In this query the dates are generated in join query.
For that i used the same table assuming it has
count of records >= the number of dates between daterange
id-s must be consecutive up to number of dates between daterange
You can replace that with your own dates list generation subquery.
set #start :='2012-05-06';
set #end :='2012-05-09';
select cdate as Date
, sum(start_date = cdate) Opened
, sum(close_date = cdate or close_date is null) Closed
, sum(start_date <= cdate and (close_date>cdate or close_date is null)) Active
from dates
join (select date(#start + interval id-1 day) cdate from dates where id <= to_days(#end)-to_days(#start)+1) d
on start_date <= cdate and (close_date>=cdate or close_date is null)
where start_date <= #end
and (close_date >= #start or close_date is null)
group by cdate
order by cdate
;
Creating an aggregate table like this is really difficult with just SQL statements. You will need to use subqueries, UNION statements and/or possibly a stored procedure to get the logic spot on so that it displays the table as you present it.
The two queries that you have already are much faster than the approach I mentioned above. A PHP array will definitely help there to get what you need.
So:
Step 1
SELECT COUNT(*) AS active_before_start
WHERE start_date < 'start'
AND close_date IS NULL
Get that number and store it in $active_before_start.
Step 2
SELECT COUNT(*) AS total_opened, start_date FROM dates
WHERE start_date BETWEEN 'start' AND 'end'
GROUP BY start_date
ORDER BY start_date ASC
Put these results in an array
foreach ($results as $result)
{
$key = $result->start_date;
$final_table[$key]['date'] = $key;
$final_table[$key]['opened'] = $result->total_opened;
}
Step 3
SELECT COUNT(*) AS total_closed, close_date FROM dates
WHERE close_date BETWEEN 'start' AND 'end'
GROUP BY close_date
ORDER BY close_date ASC
Again store those in the array
foreach ($results as $result)
{
$key = $result->close_date;
$final_table[$key]['date'] = $key;
$final_table[$key]['closed'] = $result->total_closed;
}
Step 4
Now you can traverse the table to manipulate the data so that it produces the result you need as such:
$results_table = array();
$active = $active_before_start;
foreach ($final_table as $key => $item)
{
$opened = (isset($item['opened']) ? $item['opened'] : 0;
$closed = (isset($item['closed']) ? $item['closed'] : 0;
$active = $active + ($opened' - $closed);
$results_table[$key]['date'] = $key;
$results_table[$key]['opened'] = $opened;
$results_table[$key]['closed'] = $closed;
$results_table[$key]['active'] = $active;
}
From then on you can always do a ksort just in case to show the sorted array results.
HTH
I can get it to give me how many were open on a day where that day exists in the table, but what I haven't figured out is how to make it write out a row for every day in the year and give me the count for that day.
I understand it as : you want to count the issue for each close date?
SELECT COUNT(*) FROM dates WHERE your_condition GROUP BY DAY_OF_YEAR(close_date)