i want to minimize my query
"SELECT * FROM tb_videos GROUP BY DATE(added) ORDER BY DATE(added) desc"
after get result
foreach($result as $rst)
{
$dt=date('Y-m-d',strtotime($rst->added));
SELECT * FROM tb_videos WHERE DATE(added)='$dt' ORDER BY added desc
}
Can i do with single query?
Your queries make no sense. First you are selecting the DISTINCT dates (ignore time) from the data. Then for each date, you.. select all data for that date?
Why don't you just fire one query
SELECT *, DATE(added) as DateNoTime
FROM tb_videos
ORDER BY added desc
If you only want 5 dates, and the table is large, there are two possibilities.
1, There are never gaps in dates, you can use
SELECT *, DATE(added) as DateNoTime
FROM tb_videos
WHERE added >= ADDDATE(CURDATE(), interval -4 days)
ORDER BY added desc
2, If there may be gaps, e.g. nothing for yesterday so it has to show last 5 days that have records
SELECT *, DATE(added) as DateNoTime
FROM (
select min(DateNoTime) as MinDate
from
(
select DATE(added) as DateNoTime
FROM tb_videos
order by DateNoTime desc
limit 5
) x) y, tb_videos
WHERE added >= y.MinDate
ORDER BY added desc
This gives you all the data. In PHP, keep track of DateNoTime. Whenever that changes, you are in a different date, which would have previously caused you to fire another query. The code should otherwise only change minimally.
Unchecked PHP code
$result = mysql_query('
SELECT *, DATE(added) as DateNoTime
FROM (
select min(DateNoTime) as MinDate
from
(
select DATE(added) as DateNoTime
FROM tb_videos
order by DateNoTime desc
limit 5
) x) y, tb_videos
WHERE added >= y.MinDate
ORDER BY added desc
');
$prevdate = NULL;
foreach($result as $rst)
{
if($prevdate!=$rst=>DateNoTime) {
// do something, like printing out a header for each new date
$prevdate=$rst=>DateNoTime;
}
// do something with the record
}
You want to minimise the records found? use a where clause to choose your specific records
"SELECT * FROM tb_videos WHERE videogroup = action GROUP BY DATE(added) ORDER BY DATE(added) desc"
For example
SELECT * FROM tb_videos WHERE DATE(added) = curdate() ORDER BY added desc
Related
I have records of questions stored in database. One of the column in question is the ask_date.
Example stored in is: 1549923808.
I want to select records whose ask_date is within the current month.
Please how do I calculate that in the WHERE clause of my SQL ?
//Select questions with the heighest or Top votes or answers this month( current month)
$SQL = "SELECT * FROM(
SELECT Q.*,
(SELECT COUNT(Q.question_id) FROM $questions_table Q ) num_of_rows,
(SELECT COUNT(v.vote_id) FROM $votes_table v WHERE Q.question_id=v.ask_id AND v.vote_type=0 ) votes_down,
(SELECT COUNT(a.answer_id) FROM $answers_table a WHERE question_id=a.ask_id ) total_answers,
(SELECT COUNT(v.vote_id) FROM $votes_table v WHERE Q.question_id=v.ask_id ) votes_up,
CONCAT(m.firstname,' ',m.lastname) author_name,
m.username u_name FROM $questions_table Q
LEFT JOIN $main_table m ON Q.user_id=m.user_id
WHERE ............
) A LEFT JOIN $votes_table V ON A.question_id=V.ask_id
GROUP BY A.question_id ORDER BY (
SELECT COUNT(V.vote_id) vote
FROM $votes_table V
WHERE V.ask_id=A.question_id
) DESC LIMIT {$maxPageRecords} OFFSET {$offset}";
A simple way is:
where date_format(from_unixtime(ask_date), '%Y-%m') = date_format(now(), '%Y-%m')
However, that precludes the use of indexes. So, this might be better:
where ask_date >= unix_timestamp(date(concat_ws('-', year(now()), month(now()), 1)))) and
ask_date < unix_timestamp(date(concat_ws('-', year(now()), month(now()), 1))) + interval 1 month)
select records whose ask_date is within the current month :
This simple (and index-friendly) expression should get the job done :
WHERE ask_date >= UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-01'))
Using SQL Tuples:
WHERE (year(ask_date),month(ask_date)) = (year(now()),month(now()))
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;
This is kind of straight forward.
I want to ORDER BY date DESC Limit 4 and then I want to ORDER BY date ASC on that result, so just 4-games from the middle of the big table with ASC date order, any ideas?
Just ORDER BY date ASC Limit 4 Does not work!
What I have:
What I get:
What I want:
you can use subquery :
SELECT a.* FROM (SELECT * FROM yourtable ORDER BY date DESC Limit 4) a ORDER BY a.Date
if you want to get the last four rows from table
select * from table order by date desc limit 0,4
if you want to get the first four rows from table
select * from table order by date asc limit 0,4
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
I have a mysql DB with tables, of which in the one table I have a date type field, I want the most recently passed date - so I want it to order by dates descending, but only take records from before today, and then take only the top most one using the LIMIT function, and also there is the addition of the WHERE clause being that the offer must be for the selected city.
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]."
ORDER BY exp_date DESC
LIMIT 0, 1");
ADD another condition to where clause
$result = mysql_query("
SELECT * FROM offers
WHERE city = ".$_SESSION["city"]." and Date < CURRENT_DATE()
ORDER BY exp_date DESC
LIMIT 1");
SELECT * FROM deals WHERE city = 2 AND exp_date < CURDATE()
ORDER BY exp_date DESC LIMIT 0, 1
Add the following condition to Where:
... and exp_date < CURDATE()
See http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html.