I have a column(cradit_end_date) that it specifies a product is expire or not.
this column have two states :
0000-00-00 00:00:0 <= this means this product has no expiration date.
A date (ex: 2014-10-24 10:00:00) <= this means this product has a expiration date (here almost a month later).
now, how can I get products that are both no expiration date and is not expired ?
Is it possible with PHP or SQL?
(I am using mysql).
$result_coupons = mysql_query("select id,title,pic,price,off,fake_sell,cradit_end_date from coupons where
confirm=2 and status=1 and
shop_id=$row->id ORDER BY id DESC limit 1");
You could check for multiple states in the single column using an OR operator.
...
WHERE (cradit_end_date = '0000-00-00 00:00:00' OR cradit_end_date > NOW())
...
Related
I have a table (Hire Rates) which contains multiple records. I'm trying to find records based on a date query but cannot get it to work correctly.
Essentially I am looking for records that contain a date between two dates. If my query date is "2021-08-15", it should return the third row (3000) as the query date falls between the two dates.
It is mostly working for me, except if the query date is equal to the start date or end date - in that case it doesn't return any result.
Table
startDate
endDate
hireRate
2021-01-01
2021-03-05
2350
2021-03-06
2021-04-08
2890
2021-04-09
2021-09-15
3000
Query
$sql = "SELECT rate, currencyID FROM hire_rates WHERE status = '1' AND NOT (startDate >= '$queryDate' OR endDate <= '$queryDate')
Try adding 'between' condition.
Like
select rate, currencyId from hire_rates where states =1 and not (cast($queryDate as DATE) between cast(startDate As DATE) and cast(endDate AS DATE) )
You would pass in the date you care about as a parameter. Then you can use between (based on your description of the logic):
SELECT rate, currencyID
FROM hire_rates
WHERE status = 1 AND
? BETWEEN startDate AND endDate;
Where ? is a parameter for the date you are passing in.
For 2021-08-15, you can write:
WHERE status = 1 AND
'2021-08-15' BETWEEN startDate AND endDate;
If you want the current date, you can actually get that from the database:
SELECT rate, currencyID
FROM hire_rates
WHERE status = 1 AND
curdate() BETWEEN startDate AND endDate;
Just try this then, `$sql = SELECT hireRate FROM hire_rate WHERE startDate<= '$queryDate' and endDate >= '$queryDate'
I need a web page to display items that are active and that have been dispatched less than 2 hours ago, I can't figure out the less than 2 hours ago part
I've tried to set a date variable that is 2 hours behind the current time and comparing the time dispatched to the that variable
$DateVar = date('Y-m-d h:i', strtotime('-2 hours'));
$sql="SELECT Id, TransactionId, WONumber, DispatchText, SentBy, Vendor, Dispatched,
Acknowledged, TimeDispatched, Time
FROM `DumpsterDispatch`
WHERE (Dispatched IS NULL
OR Acknowledged = 'N'
OR TimeDispatched >= \"$DateVar\")
AND Id > 1490
ORDER BY Id DESC LIMIT 100";
I expect the web page to display all active orders and any order resolved within the past 2 hours, however with this code it displays all of the resolved orders from the given day
I would suggest you'd let MySQL handle it to prevent timezone related issues. E.g.
SELECT Id, TransactionId, WONumber, DispatchText, SentBy, Vendor, Dispatched, Acknowledged, TimeDispatched, Time
FROM `DumpsterDispatch`
WHERE (
Dispatched IS NULL
OR Acknowledged = 'N'
OR TimeDispatched >= DATE_SUB(NOW(), INTERVAL 2 HOUR)
AND Id > 1490
)
ORDER BY Id DESC LIMIT 100;
Another way is using UNIX_TIMESTAMP(), it will also give the UTC timestamp, like your strtotime('-2 hours') is doing. E.g.
$dateVar = strtotime('-2 hours');
$sql = "SELECT Id, TransactionId, WONumber, DispatchText, SentBy, Vendor, Dispatched, Acknowledged, TimeDispatched, Time
FROM `DumpsterDispatch`
WHERE (
Dispatched IS NULL
OR Acknowledged = 'N'
OR UNIX_TIMESTAMP(TimeDispatched) >= {$dateVar}
)
AND Id > 1490
ORDER BY Id DESC LIMIT 100";
I figured it out!
$DateVar = date('Y-m-d h:i', strtotime('-2 hours'));
$sql="SELECT data
FROM `table`
WHERE (Dispatched IS NULL
OR Acknowledged = 'N'
OR TimeDispatched >= \"$DateVar\")
AND Id > 1490
ORDER BY Id DESC LIMIT 100";
I am having so much trouble with this. I am trying to get the maximum value per day given the range. But I keep getting a blank value in my count column when I load the query result.
<?php
include("dbconnect.php");
$link=Connection();
$data1 = '2016-04-29 00:00:00';
$data2 = '2016-05-02 00:00:00';
$result = mysql_query(
"
SELECT DATE(orderDate), MAX(Count)
FROM testLocation
WHERE orderDate
BETWEEN '$data1%' AND '$data2%'
GROUP BY DATE(orderDate)
"
,$link
);
?>
A few things to look at here.
First, the % signs in your date constants should not be there. Those only work with the LIKE operator.
Second, the BETWEEN operator works poorly for DATETIME values, because it is the equivalent of this:
WHERE orderDate >= '$data1'
AND orderDate <= '$data2' -- wrong!
This excludes everything after precisely midnight on the ending date. <= is the wrong comparison to use for DATETIME.
You should use this instead:
WHERE orderDate >= '$data1'
AND orderDate < '$data2' + INTERVAL 1 DAY
It includes everything up until, but not including, < midnight on the day after the end date you gave. That means it will include everything on the last day of your date range, which is presumably what you want.
Third, I suggest you give alias column names to the columns in your result set. That will make them easier for php code to retrieve.
SELECT DATE(orderDate) orderDate,
MAX(Count) maxCount
FROM testLocation
WHERE orderDate >= '$data1'
AND orderDate < '$data2' + INTERVAL 1 DAY
GROUP BY DATE(orderDate)
Fourth, if your testLocation table has no records in it at all for a particular date, the result set from this query will omit that date.
Fifth (others mentioned this in comments) the mysql_ API is not a good choice for your php programming; it's insecure.
I suggest you to please use PDO for the above queries.Your question is not fully clear but if you intend to retrieve database result whose value is maximum you can do the following way.
$sql='SELECT * FROM testLocation
WHERE orderDate
BETWEEN '$data1%' AND '$data2%' ORDER BY orderDate
DESC LIMIT 1';
By this query we have limited the retrieved data to be 1 and that is why you will get only the maximum value here and mark we have ordered the data in descending manner.
I am trying to make a query about joining showingdates and showings table and get all the showings for a date and order them by time.
My showingdates dayfield is formed as showdate(datetime) and shows "2013-03-06 00:00:00"
My showings performance_date field is again a datetime and examples are
"2013-03-06 20:00:00 ,
"2013-03-06 21:00:00" .
How can I make a query for that date and order them by time ?
This should work.
SELECT garbage
FROM trash
WHERE DATE(showdate) = '2013-03-06'
ORDER BY TIME(showdate) DESC
The time here is descending. Remove DESC to achieve ascending order.
To get rows only from a specific date (no use of any function on the showdate column, so indexes can be used):
WHERE showdate >= '2013-03-06'
AND showdate < '2013-03-06' + INTERVAL 1 DAY
To order by time (since all rows will have the same date, and the showdate has both date and time, simply order by that):
ORDER BY showdate
I have the following dates in my table. How do I find a closest date from either today (if today's date is there) or if today's date is not there then the nearest past date?
2012-10-01 aa123
2012-10-02 aa43
2012-10-03 aa478
2012-10-04 aa40
2012-10-05 aa54
2012-10-06 de34
2012-10-07 a5434
2012-10-08 r4t
2012-10-09 x34
2012-10-10 q23
2012-10-11 b53
So if today is '2012-10-07' is then the record will be a5434. But if 2012-10-07 is missing then the record will be de34 which belongs to 2012-10-06 since that would be the closest past day from today.
I am not sure where to start on this one, so I haven't tried anything yet. Need a sql solution to this.
It's simple, just get one of the last date <= the current date:
$now = date("Y-m-d");
$sql = "SELECT * FROM date_table where date_field <= '$now' ORDER BY date_field DESC LIMIT 1 OFFSET 1";
Add an ORDER BY statement to the query. The following will order the rows by their date, with the latest at the top and oldest at the bottom.
SELECT `id`, `date` FROM `table` ORDER BY `date` DESC LIMIT 1;