Mysql get results from todays date and forward - php

I am using opencart and wrote a new module to grab products that are coming soon. yet it still shows products even from before todays date.
my sql statement:
SELECT *, DATE_FORMAT(date_available, "%M %d, %Y") as `comingdate` FROM `product` WHERE `date_available` >= '.DATE("Y-m-d").' ORDER BY `date_available` DESC LIMIT 20
what is wrong with that statement?

"SELECT *, DATE_FORMAT(date_available, '%M %d, %Y') as `comingdate` FROM `product` WHERE `date_available` >= '".date('Y-m-d')."' ORDER BY `date_available` DESC LIMIT 20"
Remember, date_available is a string. You need the single quotes there.

Related

Select Distinct records from a Timestamp row using day only from MySQL table

I'm trying to query only distinct dates from my table (ignoring the times) which uses timestamp for the date format (should I use a better format?). Here is my query, but it doesn't seem to work:
$query = "
SELECT DISTINCT DATE(event_date)
FROM schedule
WHERE DATE(event_date) >= CURDATE()
ORDER BY event_date ASC LIMIT 4
";
"event_date" is my timestamp row in the database.
You may have a problem with the order by. How about this?
SELECT DATE(event_date)
FROM schedule
WHERE event_date >= CURDATE()
GROUP BY DATE(event_date)
ORDER BY DATE(event_date) ASC
LIMIT 4;

php - get different dates in while loop

I have this while loop:
<?php
$q=mysql_query("SELECT * FROM xeon_stats_clicks WHERE user='".$userdata['username']."' AND typ='4' ORDER BY data DESC LIMIT 8") or die(mysql_error());
while($clickData=mysql_fetch_assoc($q)):
$r=mysql_query("SELECT sum(value) FROM `xeon_stats_clicks` WHERE user='".$userdata['username']."' AND typ='3' AND data='".date("Y/m/d")."' ORDER BY data DESC LIMIT 8");
echo mysql_result($r, 0);
endwhile;
?>
This will just give me the sum of the value row for today only. How can I do so I get the data from the last 7 days?
there is no need to specify the certain date for it will give you on this specific date, another comment suggested the use of 'between' but I like the use of '>' only in this case,
for example:
SELECT sum(value) FROM xeon_stats_clicks WHERE
user=.$userdata['username'] AND type='3' AND date > CURDATE() -
INTERVAL 1 WEEK ORDER BY date DESC LIMIT 8"
SELECT SUM( value ), `data`
FROM table
WHERE ( `data` BETWEEN CURDATE() AND CURDATE() - INTERVAL 1 WEEK )
GROUP BY `data`
for example.
try another where statement:
DATEDIFF( CURDATE(), CURDATE() - INTERVAL 1 WEEK ) = 7
and show your where statement

Finding maximum revenue day

I have a table revenue with following fields
+----+-----------+-------------+---------------+
| id | member_id | amount_paid | datetime_paid |
+----+-----------+-------------+---------------+
I want to run a query to find the top 10 maximum revenue days. What would be the fastest way to achieve this? The table has a lot of data.
Regards,
Try the basic query which does it:
SELECT DATE(`datetime_paid`) AS `Max Revenue`
FROM `revenue`
ORDER BY `amount_paid` DESC
GROUP BY DATE(`datetime_paid`)
LIMIT 10;
This shows you the Top 10 Max paid days. :)
Assuming your datetime_paid column is of some date or time type (see MySQL docu), the following should work.
SELECT SUM( `amount_paid` ) AS `amount`, DATE( `datetime_paid` ) as `date`
FROM yourTable
GROUP BY `date`
ORDER BY `amount` DESC
LIMIT 10
First you need to group the results by the day the payment was made. This can be done by selecting DATE(datetime_paid) as payday, then grouping by payday. To get the total, select SUM(amount_paid) as total. Finally, to get the top 10, order by total desc and limit 10.
Your final query should look like:
SELECT DATE(`datetime_paid`) AS `payday`, SUM(`amount_paid`) AS `total`
FROM `table_name_here`
GROUP BY `payday`
ORDER BY `total` DESC
LIMIT 10
You may need to change that ORDER BY line to ORDER BY SUM(amount_paid) DESC, I can't remember if field aliases are allowed in ordering.

how to select the 5 latest row from my mysql

I wanted to know the sql command to retrieve 5 latest row from my table? Below is my sql query. How am I going to do, in order it can select the 5 latest row base on row no to be process?
$query =
"SELECT lat, lng, DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS
datetime FROM markers1 WHERE 1";
You need to order the results, and set a limit.
Assuming datetime is what you wanted to order on:
$query = "
SELECT
lat,
lng,
DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime
FROM markers1 WHERE 1
ORDER BY datetime DESC
LIMIT 5
";
EDIT:
To answer OP's comment: "result that i get is start for Row 50 for first query and it follow by 49,48,47,46 Is that possible i can get this start frm row 46,47,48,49,50 ?"
You could either do this with the result in PHP, by fetching the rows and storing them in an array and reversing the array. I don't believe you can efficiently loop through a mysql result resource in reverse.
To do this in the SQL query, you need to create a temporary table with the original query:
$query = "
SELECT
lat,
lng,
DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime
FROM (
SELECT
lat,
lng,
datetime
FROM markers1 WHERE 1
ORDER BY datetime DESC
LIMIT 5
) AS tmp_markers
ORDER BY datetime ASC
";
The result of the initial query is used as the table to search in a new query, that orders by datetime ascending. I had to apply the DATE_FORMAT on the outer query because we need the datetime field to order by again.
just add:
ORDER BY datetime DESC
LIMIT 5
The best way to do this is to add a row number and then reverse your query based on that, since we don't have any guarantees that your datetime field increments chronologically; and I'm also assuming that by 5 latest rows you mean the last 5 added to the table, not the 5 with the most recent datetime.
SELECT
lat
, lng
, DATE_FORMAT(datetime,'%W %M %D, %Y %T') AS datetime
, #rownum:=#rownum+1 `RowNum`
FROM
markers1
, (SELECT #rownum:=0) `r`
WHERE 1
ORDER BY RowNum DESC
LIMIT 5
Check out this dude's blog for the rownumber solution I used.
Might be a very late answer, but this is good and simple.
select * from table_name order id desc limit 5
This query will return a set of last 5 values(last 5 rows) you 've inserted in your table
Check out my query for last 5 entry
SELECT * FROM notices where organizationid = $orgid ORDER BY `notices`.`id` DESC LIMIT 5
So, Most Important is ORDER BY notices.id DESC LIMIT 5

Help with MYSQL query using PHP to build a blog archive navigation menu

I am building a blog archive navigation menu. Currently I run a query to get all the years and months. Then I loop through and run the following query to get all id's and titles of that year/months blog posts:
SELECT `id`, `title`
FROM `news`
WHERE YEAR(`date`) = "2010" AND MONTH(`date`) = "03"
ORDER BY `date` DESC
After that, to count the amount of posts for that month I am running a very similar query:
SELECT COUNT(*) as `count`
FROM `news`
WHERE YEAR(`date`) = "2010" AND MONTH(`date`) = "03"
The year and month are dynamic of course.
Is there any way to avoid having to run two separate queries?
Thanks ahead of time!
What's wrong with:
SELECT `id`, `title`, count(*) AS `count`
FROM `news`
WHERE YEAR(`date`) = "2010" AND MONTH(`date`) = "03"
GROUP BY ( `id` ) #assuming id is unique
ORDER BY `date` DESC
?
EDIT:
Forgot to add GROUP BY clause
EDIT 2:
Forget the above. That was obviously not correct. This will do the trick, though it may be concidered not very elegant:
SELECT
`id`,
`title`,
( SELECT
count(*)
FROM
`news`
WHERE
YEAR(`date`) = "2010" AND
MONTH(`date`) = "01" ) as `count`
FROM
`news`
WHERE
YEAR(`date`) = "2010" AND
MONTH(`date`) = "01"
ORDER BY
`date` DESC
So maybe the suggested mysql_num_rows() isn't so bad after all. :)
You can count the rows via php after sending the first query (http://php.net/manual/en/function.mysql-num-rows.php)
You could do it using php count();
If your db Implements this methods :
echo count($this->db->fetch_array($q));
it will be easy to get know how many items there are
Can't you do all of this in one shot?!
SELECT COUNT(id) as count, MONTH(date) as month, YEAR(date) as year
FROM news
GROUP BY MONTH(date), YEAR(date)

Categories