MySQL get date of record where count was achieved - mysql

This honestly sounds like a job for a function in MySql but I'm wondering if there's a way to make a query that selects the date of the record that achieves the count = x
Setup: 1000 records each having the same qualifying conditions.. lets say user_id and visit information and a create_date
Desired Query result: Select the date of the 100th visit
SELECT create_date
FROM user_visits
HAVING COUNT(id) = 100;

You can use order by on your auto_increment column and limit 99,1 to pick 100th visit
SELECT create_date
FROM user_visits
ORDER BY your_auto_increment_column
LIMIT 99,1

Related

SQL: How do I search the data with the LATEST date AND the MOST value in quantity_used?

I know how to search the LATEST date and the MOST value specifically:
Most Quantity Used:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY quantity_used DESC
Latest Date:
SELECT * FROM tour_packages
WHERE active = 1
ORDER BY start_date DESC
But how can I do both, by able to search the LATEST date WITH the MOST value in quantity_used? Is this practice even possible?
EDITED: I think my question is not clear enough.
I intend to find the data with the LATEST date first, then from that result FIND the highest VALUE from quantity_used.
I think you just want two order by keys:
SELECT tp.*
FROM tour_packages tp
WHERE tp.active = 1
ORDER BY tp.start_date DESC, tp.quantity_used DESC;
This returns the rows ordered by date and within each date, the ones with the largest quantity go first.

Getting last 30 days of records

I have a table called 'Articles' in that table I have 2 columns that will be essential in creating the query I want to create. The first column is the dateStamp column which is a datetime type column. The second column is the Counter column which is an int(255) column. The Counter column technically holds the views for that particular field.
I am trying to create a query that will generate the last 30 days of records. It will then order the records based on most viewed. This query will only pick up 10 records. The current query I have is this:
SELECT *
FROM Articles
WHERE DATEDIFF(day, dateStamp, getdate()) BETWEEN 0 and 30
LIMIT 10
) TOP10
ORDER BY Counter DESC
This query is not displaying any records, but I don't understand what I am doing wrong. Any suggestions?
The MySQL version of the query would look like this:
SELECT a.*
FROM Articles a
WHERE a.dateStamp >= CURDATE() - interval 30 day
ORDER BY a.counter DESC
LIMIT 10;
Your query is generating an error. You should look at that error before fixing the query.
The query would look different in SQL Server.

How to select and select count in one query in mysql?

I have this table:
Activity Date
reading 12-10-2014
watching 12-10-2014
reading 13-10-2014
reading 12-10-2014
watching 13-10-2014
What I want to do is selecting the activity and count the activity number based on date, I want the output will be like this (with condition: where date ='12-10-2014'):
Activity count date
reading 2 12-10-2014
watching 1 12-10-2014
How can I do that?, help me please. Thanks.
SELECT Activity,COUNT(1) `count`,date
FROM mytable WHERE date='12-10-2014'
GROUP BY date,Activity;
or
SELECT Activity,COUNT(1) `count`,date
FROM mytable WHERE date='2014-10-12'
GROUP BY date,Activity;
Make sure you table has an index on date and Activity
ALTER TABLE mytable ADD INDEX date_Activity_ndx (date,Activity);
This should work:
select activity, count(activity) as count, date from my_table
where date = '12-10-2014' group by activity;
(not sure about your column labels--you may have to adjust for capitalization)

How ORDER BY works in MySQL?

assume there is a table with 100 million records in it with this schema:
+---------+---------+------+
| user_id | post_id | date |
+---------+---------+------+
We want to have latest 30 records which user with #1 is posted.
SELECT * FROM users_posts WHERE user_id = 1 ORDER BY date DESC LIMIT 30
How MySQL search in table ? Does it search all rows ( full scan ) finding all matches THEN ordering them by date?
if Yes( Full Scan ), how can we edit this query to limit it to search latest records till finds 30 matches. ( to increase reading speed and have better performance )
thanks in advanced.
If you want the best performance for this query:
SELECT *
FROM users_posts
WHERE user_id = 1
ORDER BY date DESC;
Then you want an index on users_posts(user_id, date). This will allow the SQL engine to only use the index to define the set of rows being used. First, it will use user_id to satisfy the where clause. Then it will use the date part for the order by. It will still have to look up the records in the original data pages, in order to get all the columns needed for the select.
If you want to limit this to 30 rows, then add limit 30:
SELECT *
FROM users_posts
WHERE user_id = 1
ORDER BY date DESC
LIMIT 30;
With the above index, this will improve performance. With no index, the engine will have to scan all the data, sort the data that matches the where clause, and then apply the limit -- in other words, it won't be much of an improvement in performance.

Mysql query list results matching each date

Not sure how this would work. I have a between query, but how would I run a query to list results that match each and every day. Example, enterys that exists on 2011-06-17, 2011-06-18, 2011-06-19 and 2011-06-20
SELECT lookup, `loc`, `octect1` ,`octect2` ,`octect3` ,`octect4`, date, time, count(`lookup`) as count FROM index
WHERE date between '2011-06-17' AND '2011-06-20'
GROUP BY lookup
ORDER BY count DESC
Thanks
Instead of BETWEEN, use comparison operators:
SELECT lookup, `loc`, `octect1` ,`octect2` ,`octect3` ,`octect4`,
date, time, count(`lookup`) as count
FROM index
WHERE date > '2011-06-17' AND date < '2011-06-20'
GROUP BY lookup
ORDER BY count DESC