I am trying to build an archive list for a blog using php and mysql. The problem is I am not sure of the best way to do this.
I was hoping there was a way to get a list of years and than display them so lets say my table has id | year | content and has the current rows
1 | 2013 | content
2 | 2013 | content
3 | 2013 | content
4 | 2012 | content
5 | 2012 | content
6 | 2011 | content
is it possible to make a mysql statement that well only return IDs 1,4,6, if so how?
try this
select * from table
group by year
if you consider the order then add this in the end order by id
DEMO here
you can also get the specified Id by using Min or Max. like this
select min(id) ,year ,content from table
group by year
demo
select min(id) , year from table group by year
To just get a list of the 'year's in your table:
select distinct year from table
Depending on the size of your table and how often you're running the query, you could think about indexing on 'year'.
Related
Hi I have some file's records with the number like 01/2020, 02/2020 up to so on and I have used order by clause to get the records in order by these numbers but it return correctly from 01/2020 to 10/2020 then it show me 100/2020 is there a solution? kindly share with me.
If my assumption is correct whereby the format of this record is running_no/year then you need to extract the year to include in order by. Consider example below.
If data is like this:
+----------+
| Value |
+----------+
| 01/2020 |
| 02/2020 |
| 10/2020 |
| 100/2020 |
| 01/2021 |
| 10/2021 |
+----------+
Whereby the 4 digits at the back represent running year, then you can extract the year from if in a few ways. Here I'm showing two example using RIGHT() and SUBSTRING_INDEX().
example 1:
SELECT * FROM table ORDER BY RIGHT(Value,4) ASC, ABS(Value) ASC;
example 2:
SELECT * FROM table ORDER BY SUBSTRING_INDEX(Value,'/',-1) ASC, SUBSTRING_INDEX(Value,'/',1) ASC;
There are more ways to achieve this as long as you can be certain it will return the result the way you want.
My problem has been solved by using ORDER BY ABS(MyField)
hi I'm trying to create a chart with php and mysql to show daily number of posts that generate Automatically using mysql queries.in database just we have date column.
I want some thing like this
date | posts count
------------+---------------
2017/05/02 | 5
2017/05/03 | 0
2017/05/04 | 1
2017/05/05 | 2
2017/05/06 | 0
2017/05/07 | 3
how to do this?
First of all you need to learn how to use these two:
Group by
COUNT
Since I have no idea about your table architecture here is a sample that you can adjust to your case:
select p.datePost, count(p.id) as postcounts
from posts p
group by p.datePost
I have a scraper which periodically scrapes articles from news sites and stores them in a database [MYSQL].
The way the scraping works is that the oldest articles are scraped first and then i move onto much more recent articles.
For example an article that was written on the 1st of Jan would be scraped first and given an ID 1 and an article that was scraped on the 2nd of Jan would have an ID 2.
So the recent articles would have a higher id as compared to older articles.
There are multiple scrapers running at the same time.
Now i need an endpoint which i can query based on timestamp of the articles and i also have a limit of 10 articles on each fetch.
The problem arises for example when there are 20 articles which were posted with a timestamp of 1499241705 and when i query the endpoint with a timestamp of 1499241705 a check is made to give me all articles that is >=1499241705 in which case i would always get the same 10 articles each time,changing the condition to a > would mean i skip out on the articles from 11-20. Adding another where clause to check on id is unsuccessful because articles may not always be inserted in the correct date order as the scraper is running concurrently.
Is there a way i can query this end point so i can always get consistent data from it with the latest articles coming first and then the older articles.
EDIT:
+-----------------------+
| id | unix_timestamp |
+-----------------------+
| 1 | 1000 |
| 2 | 1001 |
| 3 | 1002 |
| 4 | 1003 |
| 11 | 1000 |
| 12 | 1001 |
| 13 | 1002 |
| 14 | 1003 |
+-----------------------+
The last timestamp and ID is being sent through the WHERE clause.
E.g.
$this->db->where('unix_timestamp <=', $timestamp);
$this->db->where('id <', $offset);
$this->db->order_by('unix_timestamp ', 'DESC');
$this->db->order_by('id', 'DESC');
On querying with a timestamp of 1003, ids 14 and 4 are fetched. But then during the next call, id 4 would be the offset thereby not fetching id 13 and only fetching id 3 the next time around.So data would be missing .
Two parts: timestamp and id.
WHERE timestamp <= $ts_leftoff
AND ( timestamp < $ts_leftoff
OR id <= $id_leftoff )
ORDER BY (timestamp DESC, id DESC)
So, assuming id is unique, it won't matter if lots of rows have the same timestamp, the order is fully deterministic.
There is a syntax for this, but unfortunately it is not well optimized:
WHERE (timestamp, id) <= ($ts_leftoff, $id_leftoff)
So, I advise against using it.
More on the concept of "left off": http://mysql.rjweb.org/doc.php/pagination
I have a table ("comments") full of comments, the date they were created, and, if they were pushed to the front via a PHP button, the date of that also (otherwise the date will be '0000-00-00'). What I'd like is the table to be displayed as normal, but to have the comments that were "repushed" to be pushed to a place within the table with an ORDER BY of date created ("created").
Here's an example of what I'd like to achieve, which involes pushing an old comment (ID: 4) to the top (NOTE: there are no duplicates ID's in the table, only the query result will duplicate repushed records as shown here):
id | comment | created | repushed
---+---------+---------------------+-------------------
4 | hello | 2015-05-11 06:11:12 | 2015-11-22 11:17:01
23 | recent | 2015-05-22 12:18:23 | 0000-00-00 00:00:00
22 | recent | 2015-05-22 11:15:43 | 0000-00-00 00:00:00
21 | recent | 2015-05-22 10:23:10 | 0000-00-00 00:00:00
4 | hello | 2015-05-11 06:11:12 | 2015-11-22 11:17:01
So I'm trying to use UNION to combine all comments with repushed comments (so all repushed comments will appear duplicated once queried), but then I want to treat the duplicated repushed comments as if they were created at the time they were repushed, so I would need to swap the "created" and "repushed" column values of duplicates to get this effect, and then order by "created".
SELECT *
FROM comments
WHERE repushed != '0000-00-00 00:00:00'
UNION ALL
SELECT *
FROM comments
ORDER BY created DESC
Of course this doesn't work as intended since I don't know how to swap the two column values around.
Working with what you already have try this:
SELECT id,comment,repushed as created,repushed
FROM comments
WHERE repushed != '0000-00-00 00:00:00' group by id
UNION ALL
SELECT *
FROM comments group by id ORDER BY created DESC
This will rename the column repushed to created
I have a table clicks like this
+-----+-----+-------+-------+
| id | time | site |
+-----+-----+-------+-------+
| 1 | 8 4 2013 | site1 |
| 2 | 8 4 2013 | site1 |
| 3 | 9 4 2013 | site2 |
| 4 | 6 4 2013 | site1 |
+-----+-----+------+--------+
I want to show result like this
Date | Count
8 4 2013 | 2 click
6 4 2013 | 1 click
i have this code
$id = 'site1';
$getclk = mysql_query("SELECT * FROM clicks WHERE site='$id'");
$clk = mysql_num_rows($getclk);
print $clk;
this code show only count of rows and i want to show count of row grouped by time.
GROUP BY command works for this but i don't know how to do this.
please help me i dont know how to do this, please give me full code to do this.
I think it should be:
SELECT COUNT (id) as countClicks, time as Date
FROM clicks
[WHERE site = ?]
GROUP BY time
ORDER BY countClicks DESC
Also, you shouldn't use mysql_* functions because they are deprecated.
$getclk="SELECT date,count(*) as c FROM Clicks WHERE site='$id' GROUP BY date ORDER BY c DESC";
Then loop through the results and display them. please give me full code to do this. So Sorry, not the right forum for that
SELECT time, COUNT(*) as count FROM clicks GROUP BY time
This should give you result in the format you've asked for:
$getclk = mysql_query("select time as Date, concat(count(*), ' click') as Count
from Clicks
where site = '$id'
group by time
order by Count desc");
Please note the use of mysql function concat() function to append click text to number of records grouped by time under Count column would be better if you appended this text in php instead of mysql so that less data would be transferred from the database server.
As already made aware by other answerers, mysql_ functions are deprecated, you should be using either mysqli or pdo for new code.
write Your query like below
$id = 'site1';
$getclk = mysql_query("SELECT count(id) as clcount,time FROM clicks WHERE site='$id' group by time order by time");
$clk = mysql_num_rows($getclk);
print_r($clk);