mysql query cant order records as needed - mysql

My table is:
+----visits----+
| id |
| client_ip |
| date |
| type |
+--------------+
It stores all site user visits. Now i need to make a graph by getting data from this table, my query is:
SELECT COUNT(*) as hit_counts, date FROM visits GROUP BY date ORDER BY date ASC LIMIT 25
But the idea is that each time i refresh a page on my site, i get a new row inserted in table, and mysql counts 25 records(but i need to limit my query at 25 days(1 day can have 100+ visits), not only 25 records).

Try this:
SELECT temp_table.* FROM
(SELECT COUNT(*) as hit_counts, date
FROM visits
GROUP BY date) as temp_table
ORDER BY temp_table.date ASC
LIMIT 25
use DESC for the last 25 days.

Related

How can I get the last entry in a table with a specific ID

I have a MySQL table with 5 columns which are:
+----+----------+--------------+---------------+------+
| id | DeviceID | PossibleLeak | ConfirmedLeak | Time |
+----+----------+--------------+---------------+------+
and I want to select the last entry with a specific DeviceID.
Is there a query I can use for that.
Values are being inserted in there and the id column is auto-incremented.
Filter your data on the specific DeviceID, order the data in descending order and get only the first record
select *
from your_table
where DeviceID = 1234
order by id desc
limit 1

How to select records from mysql along with one previous record

I have table like this
+----+----------------------+------------+
| id | desc | date |
+----+----------------------+------------+
| 15 | nah_i_kid | 2017-06-07 |
+----+----------------------+------------+
| 17 | it_is_just_the_cat | 2017-06-08 |
+----+----------------------+------------+
| 18 | thank_God | 2017-06-09 |
+----+----------------------+------------+
| 44 | no_kidding | 2017-06-10 |
+----+----------------------+------------+
My sql is
SELECT * FROM TABLE WHERE date between '2017-06-09' AND '2017-06-12'
I want the result should contain one previous record also (i-e record having id=17 take it example)
Thanks.
If you are using MYSQL, I have tried and it work good.
(select * from table where date < '2017-06-09' order by date desc limit 1 ) union (select * from table where date between '2017-06-09' AND '2017-06-12' order by date)
If you want the records from the previous date, you can do:
select t.*
from t
where date > (select max(t2.date) from t t2 where t2.date < '2017-06-09') and
date <= '2017-06-12';
This does what you want, assuming you have no duplicates on a date.
If you want exactly one row and you know the ids are assigned in chronological order, you can do:
select t.*
from t
where id > (select max(t2.id) from t t2 where t2.date < '2017-06-09') and
date <= '2017-06-12';
This solves the problem by taking the most recent previous record based on id.
If the ids are not in chronological order and you can have duplicates, the query gets more difficult. There is no definition of the "previous record". A union all is the best solution:
(select t.*
from t
where date < '2017-06-09'
order by date desc
limit 1
) union all
select t.*
from t
where date > >= '2017-06-09' and
date <= '2017-06-12'
I think this is what you're looking for.
The first part of the query is identical to yours, the second part gets all the values before your first date 2017-06-09, orders the values by date DESC, then limits the query to only take the topmost value using LIMIT 1.
SELECT
*
FROM table
WHERE `date` BETWEEN '2017-06-09' AND '2017-06-12'
OR `id` = (
SELECT
`id`
FROM table
WHERE `date` < '2017-06-09'
ORDER BY `date` DESC
LIMIT 1
)
I want the result should contain one previous record also (i-e record having id=17 take it example)
If you know that the rows are created in chronological order, and your id field is auto-increment, then you don't even need to use the date field, because you can assume that a higher id indicates a later record. So just cap your search on the id you want, and grab two rows:
SELECT * FROM TABLE WHERE id <= 17 ORDER BY id DESC LIMIT 2;
This has the added benefit of being fully indexed, which may not be the case if you introduce a WHERE clause on the date field.

How to extract the minimum or maximum last recent row from the table using mysql?

My Mysql database:
| time | Name | TYU
| 1483347398 | vprasad | PDSP_DES
| 1483348583 | akawle | LPT
**My Query:
I had used the (select * from users) query to retrieve the above table.Now i need to output the entire row from the database according to the minimum or maximum time from the table name.
So far i tried:
i had tried the following query (select min(time) from users).i dont to know how to retrieve the entire row using min(time) or max(time) using mysql.
My output:
min(time) |
+------------+
| 1482484399 |
+------------+
My expected output:
| 1483348583 | akawle | LPT
You can use sorting method.
SELECT * FROM users ORDER BY time ASC LIMIT 1
sort by ASC to get smallest as first result
limit 1 so you get only 1 result (the smallest)
Here you go
SELECT time, name, TYU FROM users WHERE time = (select min(time) from users)
as your output try this:-
for max() time
select time,Name,TYU from tt where time = (select max(time) from tt);
for min() time
select time,Name,TYU from tt where time = (select min(time) from tt);
check on fiddle:-here
you should try this:
SELECT min(time) as min_time,name,TYU FROM users.
it would result like :
1483347398 | vprasad | PDSP_DES
because in (1483348583 and 1483347398), 1483347398 is minimum.
OR
if you want result like:
1483348583 akawle LPT
you should try this:
SELECT max(time) as min_time,name,TYU FROM users.
OR
if you want all rows then you can try like this:
SELECT time,name,TYU FROM users order by time asc.

sort on two columns

I have a table with site visits with the following columns:
timestamp
host
url
I would like to see the last few (say 200) visits but at the same time group visits of the same host together (to see the sequence of pages he/she visits).
If I do:
... SORT BY timestamp DESC, host
I get hosts mixed up if several visitors are online at the same time
If I do:
... SORT BY host, timestamp DESC
I do not get the latest visits (just the latest visits from each host)
I would like to see e.g.
9:01 | a.com | /index
9:05 | a.com | /interesting
9:07 | a.com | /contact
9:02 | b.com | /index
9:03 | b.com | /product-a
9:08 | b.com | /thanks-for-buying
Is that possible in Mysql or should I further process the result in php?
Try this::
Select *, MAX(timestamp_col) as lastVisit from site_table
GROUP BY HOSTS order by lastVisit desc
You can't do ORDER BY followed by LIMIT and then another ORDER BY, so you have to use a sub query:
SELECT * FROM (SELECT *
FROM visits
ORDER BY timestamp DESC
LIMIT 200
) visits2
ORDER BY host
You have to limit the result if you want the last 200
SORT BY timestamp DESC, host LIMIT 0, 200
And if you dont the same visitor to show up again , use
SELECT DISTINCT visitor FROM ....
I think it is not possible make normal in one query. You can try this:
SELECT *
FROM table AS t1
WHERE t1.timestamp_col >= (SELECT MIN(tstamp)
FROM (SELECT t2.timestamp_col
FROM tables AS t2
WHERE t2.host = t1.host
ORDER BY timestamp_col DESC
LIMIT 200
)
)
I don't test this crazy code... But I hope it give you way :)
The answers above pointed my in the right direction, thanks. This one works. The LIMIT 1000 is to reduce the query from almost 3 minutes to 7 seconds (40000 records).
SELECT v1.stamp, v1.host FROM visits AS v1
LEFT JOIN (SELECT MIN(date) AS firstVisit, host
FROM visitor AS v2
GROUP BY v2.host
ORDER BY firstVisit DESC
LIMIT 1000) AS j
ON v1.host=j.host
ORDER BY firstVisit DESC, v1.host, v1.date DESC
LIMIT 100;

How do I count the number of visits to my site on a date with mysql?

I am storing all visits to my site in a table, I store the date, the page visited and a session id.
In theory, I can group somebody by their session id and this counts as 1 visit.
What I'd like to do however is go through the table and get the total of visits for each date. So it would group by the session id, and then group by the date.
ie:
SELECT DATE(added) as date, COUNT(*) FROM visits GROUP BY sessionID, date
This doesn't work as it retrieves then the total of visits for that session id, and the date.
My table structure looks a bit like this:
----------------------------------
| id | added | page | sessionid
----------------------------------
Any ideas?
My query gives me results that look like this:
2010-11-24 | 2
2010-11-24 | 14
2010-11-24 | 17
2010-11-24 | 1
While I'd be hoping for something more like a total of all those under the 1 date, ie:
2010-11-24 | 34
Each date contains the time which will be different for each request. If you use DATE in the GROUP BY clause just like you did in the SELECT clause, that will solve your problem.
By grouping by sessionID, it's going to create a row for every session. If instead of grouping by sessionID, you use COUNT(DISTINCT sessionID), that will contact the distinct number of session IDs for that date.
SELECT DATE(added) as date, COUNT(DISTINCT sessionID) as sessions FROM visits GROUP BY DATE(added)