extract less than the limit value mysql - php

i try to display only the articles submitted in the last day, limiting to 5, but i have a problem, if in a category exist less then 5 articles, i don't want to display duplicate, display only that 1,2,3,4 articles, how to do this? Thanks!
$Time=time();
$LimitDay=$Time - 86400;
$SelectArticle=mysqli_query($ConnecDB, "SELECT * FROM mk_sn_article WHERE art_category='$Display[CategorieMail]' AND art_data BETWEEN '$LimitDay' AND '$Time' ORDER BY art_id DESC LIMIT 5");

SELECT * FROM mk_sn_article WHERE art_category='$Display[CategorieMail]' AND art_data > DATE_SUB(NOW(), INTERVAL 1 DAY) ORDER BY art_id DESC LIMIT 5
Just use DATE_SUB with a comparison - it will pull all -24h articles

Related

Mysql get data by year with pagination

I need to get the data based on year with pagination,if the rows count is less,then search in next year
SELECT *
FROM `user_notifications`
WHERE DATE_FORMAT(created_at, '%Y') = '2019'
ORDER BY `created_at` DESC
LIMIT 0,10
if rows count is less than 10,then search by year 2018
Given that your results are sorted by created_at DESC, I don't think a WHERE clause is necessary at all. If there are insufficient results from 2019, your query will automatically return results from 2018, 2017, 2016 etc. as necessary to get to 10 rows:
SELECT *
FROM `user_notifications`
ORDER BY `created_at` DESC
LIMIT 10
I think you just need a WHERE clause, to include both 2018 and 2019:
SELECT *
FROM user_notifications
WHERE YEAR(created_at) IN (2018, 2019)
ORDER BY created_at DESC
LIMIT 10;
If I've read it right, this sounds like just a simple ordering exercise
SELECT *
FROM user_notifications
WHERE created_at < :input_year + INTERVAL 1 YEAR
ORDER BY created_at DESC
LIMIT :offset, 10;
If you only want to go back one year you can just add another condition
SELECT *
FROM user_notifications
WHERE created_at < :input_year + INTERVAL 1 YEAR
AND created_at >= :input_year - INTERVAL 1 YEAR
ORDER BY created_at DESC
LIMIT :offset, 10;
Not that using a function like YEAR() on created_at rather than a comparison (<,>=) will prevent the engine from using an index on created_at
Use https://dev.mysql.com/doc/refman/8.0/en/year.html
SELECT *
FROM user_notifications
WHERE YEAR(created_at) <= $searchYear
ORDER BY created_at DESC
LIMIT 0, 10;

mysql fetch records which have been added in the last 10 seconds?

I want to fetch records that have been added in the last 10 seconds. I have a "zaman" column which has the timestamp of the record that tells when it has been added.
I have tried this one, however, it doesnt work (no errors)
mysql_query("SELECT * FROM notifs where writer='".$member[nick]."' AND
zaman >= DATE_SUB(NOW(),INTERVAL 10 SECOND) ORDER BY id DESC limit 5")
or die(mysql_error());
What is the correct way to do it ?
When a new record is saved, record it's time saved. Then, when running the fetch query, get all values that are greater than or equal to current time - 10.
assuming zaman is a epoche timestamp...
mysql_query("SELECT * FROM notifs where writer='".$member[nick]."' AND
zaman >= UNIX_TIMESTAMP()-10 ORDER BY id DESC limit 5")
try this
mysql_query("SELECT * FROM notifs where writer='".$member[nick]."' AND zaman >= (NOW() - INTERVAL 10 SECOND) ORDER BY id DESC limit 5")

Select count from a table with a Where on another table

I have two tables
oc2_visits (fields: id_ad)
oc2_ads (fields: published)
I need to count the visits by grouping the id_ad, and that works, but I need also that the first 9 results have been published not more that 25 days ago.
This is the query I have at the moment:
SELECT count(v.id_ad) AS visits,
v.id_ad,
a.published
FROM oc2_visits AS v,
oc2_ads AS a
WHERE DATE(a.published) >= DATE_SUB(NOW(), INTERVAL 25 DAY)
GROUP BY v.id_ad
ORDER BY visits DESC LIMIT 0,9
but when I try to enter the query in phpmyadmin, it crashes.
What am I doing wrong?
I think you forgot to parse DATE_SUB(NOW(), INTERVAL 25 DAY) into DATE while comparing with a DATE. Hope it'll work.
SELECT count(v.id_ad) AS visits,
v.id_ad,
a.published
FROM oc2_visits AS v,
oc2_ads AS a
WHERE DATE(a.published) >= DATE(DATE_SUB(NOW(), INTERVAL 25 DAY))
GROUP BY v.id_ad
ORDER BY visits DESC LIMIT 0,9
Presumably the root of your problem is the cartesian product between two tables. Simple rule: Never use comma in the from clause. Always use explicit join syntax.
I imagine the query you want looks something like this:
SELECT count(v.id_ad) AS visits, v.id_ad, a.published
FROM oc2_visits v join
oc2_ads a
ON v.id_ad = a.id_ad
WHERE DATE(a.published) >= DATE_SUB(NOW(), INTERVAL 25 DAY)
GROUP BY v.id_ad
ORDER BY visits DESC
LIMIT 0, 9;

MySQL query - select entries not older than a time period

I have this query
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
The key part of my query is date < DATE_SUB(CURDATE(), INTERVAL 1 WEEK). It returns me entries older than a week. What i want it to return me is entries NOT older than 1 week. How can i modify it to return me desired result?
Thank you.
Have you tried with
SELECT * FROM content
WHERE topic='$rw09[id]' AND active='1' AND date > DATE_SUB(CURDATE(), INTERVAL 1 WEEK)
ORDER BY cpc DESC, id DESC
LIMIT 4
?

PHP / MySQL - Construct a SQL query

Im having a little trouble constructing a query.
I have a table with 3 columns.
id - day - pageviews
What i basically want to do is get 8 id's from the table where the pageviews are the highest from the last 60 days.
The day column is a datetime mysql type.
Any help would be great, im having a little trouble figuring this one out.
Cheers,
Almost the same as TuteC posted, but you'll need a group by to get what you need...
SELECT id, SUM(pageviews) totalViews
FROM table
WHERE DATE_SUB(CURDATE(), INTERVAL 60 DAY) <= day
GROUP BY id
ORDER BY totalViews DESC
LIMIT 8
Do something like this:
SELECT id FROM table_name
WHERE DATE_SUB(CURDATE(),INTERVAL 60 DAY) <= day
ORDER BY pageviews DESC
LIMIT 8;
$sixtyDaysAgo = date('Y-m-d',strtotime('-60 days'));
$sql = "SELECT id
FROM table_name
WHERE day >= '$sixtyDaysAgo 00:00:00'
ORDER BY pageviews DESC
LIMIT 8";
If each row is a number of pageviews for that day, and you're looking for the highest total sum of 60 days' worth, then you'll need to total them all and then grab the top 8 from among those totals, like so:
$sql = "SELECT id
FROM (
SELECT id, SUM(pageviews) AS total_pageviews
FROM table_name
WHERE day >= '$sixtyDaysAgo 00:00:00'
GROUP BY id
) AS subselect
ORDER BY total_pageviews DESC
LIMIT 8";

Categories