mysql select most recent, limit by source - mysql

I have a table with the following fields:
id
source_id
title
date
I want to select the 25 most recent items, so SELECT * FROM table ORDER BY date DESC LIMIT 50
The extra requirement is to select only the 3 most recent from every source_id.
So if the records look something like that,
id | source_id | title | date
----+-----------+-------+---------
1 2 aaa 2012-1-1
2 2 aaa 2012-1-2
3 2 aaa 2012-1-3
4 2 aaa 2012-1-4
5 3 aaa 2012-1-5
6 4 aaa 2012-1-6
I want my query to return items 4,3,2,5,6
So just the 3 most recent of every source with an over all limit of 25.
I'm not sure it's clear enough so please ask if you need more details.

Here you go:
SELECT *
FROM your_table t1
WHERE
(
SELECT COUNT(*)
FROM your_table t2
WHERE
t1.source_id = t2.source_id
AND t1.date < t2.date
) < 3
ORDER BY source_id, date DESC
Result:
4 2 aaa 2012-01-04
3 2 aaa 2012-01-03
2 2 aaa 2012-01-02
5 3 aaa 2012-01-05
6 4 aaa 2012-01-06
In plain English: take only rows that have less than 3 newer rows with the same source_id.
NOTE: This could select more than 3 rows per source_id if the third newest date (for the same source_id) happens to be shared by more than one row. Let me know what "3 newest" means in this context if that's a problem...

select * from table where source_id in
(select distinct source_id from table order by date limit 3)
LIMIT 25

Related

MySQL: Find "same" Data

I am currently in the need to find entrys matching the same pattern in a connection table.
The Table looks like
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
3 1 1 5
4 1 2 4
5 5 1 3
6 5 2 7
so my basic information is the data of job_id 15
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
I want to find job_id 5 because the data in ext_id and data1 is the same as in job 15. the data of job_id 1 differs, so I don't want to find that.
Any idea on how to do it?
I believe you want this:
select *
from your_table
group by data1,
ext_id
having count(*) > 1
This post explains it:
How to find duplicates in 2 columns not 1
EDIT
I believe this should return all rows that have mathcing data1 and ext_id values
select * from table t1
INNER JOIN table t2 ON t1.data1=t2.data1 and t1.ext_id=t2.ext_id

how can I group by elements from one table and group by it by 2 columns?

I have a table with a structure like this:
table_id | user_id | text | number_id | start_time
1 1 gdsgds 8 2015-10-11 07:14:44
2 2 vcxvc 8 2015-10-11 07:20:44
3 4 ewgs 8 2015-10-12 09:19:22
4 7 vvvcc 8 2015-10-13 18:12:23
etc.
I need to write a query that will return me the number of texts displayed each day on each number. So far I have the following query:
SELECT *
FROM
(
SELECT DATEDIFF(now(), start_time) AS days_ago,
COUNT(table_id) AS num_texts
FROM TABLE
GROUP BY DATE(start_time)
)
WHERE
(
days_ago <= 7
AND days_ago > 0
)
and this query returns me a table:
days_ago | num_texts
0 | 2
1 | 3
2 | 4
3 | 1
and that works almost fine, but I need to divide it by number_id too... How can I do it?
Add the column number_id to the select and group by in the subquery.
SELECT *
FROM
(
SELECT DATEDIFF(now(), start_time) AS days_ago,
number_id,
COUNT(table_id) AS num_texts
FROM TABLE
GROUP BY DATE(start_time), number_id
)
WHERE
days_ago <= 7
AND days_ago > 0

How to select more rows with MySQL after sorting?

I have a table which have 10 results. Let's say the following:
id user number
-- ---- ------
1 user1 10
2 user2 5
3 user3 30
4 user4 45
5 user5 5
6 user6 22
7 user7 10
8 user8 40
9 user9 90
10 user10 65
I basically want to sort them, by the 'number' value.
So it should be something like this:
SORT id user number
---- -- ---- ------
1 2 user2 5
2 5 user5 5
3 1 user1 10
4 7 user7 10
5 6 user6 22
6 3 user3 30
7 8 user8 40
8 4 user4 45
9 10 user10 65
10 9 user9 90
After it's been sorted, I want to select * from (for example) the one with id = 6 (which's number is 22) and 2 other results which is above it (in this case: id = 7 and 1) and 2 other results under it (in this case: id = 3 and 8).
So the return result should be something like this, when I'm searching for id = 6:
SORT id user number
---- -- ---- ------
3 1 user1 10
4 7 user7 10
5 6 user6 22
6 3 user3 30
7 8 user8 40
I could esaily do this on server side, if I select everything, however there will be a huge data amount in here, so I'd rather just select those, which are appropriate to my search.
Is there any way to do this with MySQL?
Here is a typical way to get what you want:
select t.*
from ((select t.*
from table t
where number <= (select number where id = 6 limit 1)
order by number desc
limit 3
) union all
(select t.*
from table t
where id > (select number where id = 6 limit 1)
order by number asc
limit 2
)
) t
order by number;
This assumes that when duplicates appear, you still want 5 rows output. It also assumes that less than five rows is ok for the first two or last two rows. An index on id would help performance of this query.
Get UNION of 2 queries:
Get all records <= the desired number, in your case 22. Sort them in descending order and get top 3 results.
Get all records > the desired number in ascending order and get top 2 results
Get the UNION of these 2 results and order them in ascending order
HTH
EDIT: My idea to post this answer was to give an algorithm on how to approach such queries. Instead of selecting on number, you can get all records based on id and then UNION them.

How to select the highest sum of value in mysql

I have the table like this
id rate_user_id content_id points category_id
1 100 1 5 1
2 101 1 3 1
3 100 2 8 1
4 103 2 11 1
So I want to looking highest point of content in this category 1
Content_id 2 = 19 points .
U can do this by
select content_id,sum(points) from user group by content_id order by sum(points) desc limit 1;
Thanks
have a look at the command MAX.
http://technet.microsoft.com/en-us/library/ms187751.aspx
Something like
SELECT MAX(Pts) FROM
(SELECT SUM(points) Pts GROUP BY content_id) A

Getting only 1 record from each category for each page in pagination

My table structure is as follows
id int
name varchar 50
catid int
Sample data
id name catid
---------------------------------------------------------
1 AAA 1
2 BBB 1
3 CCC 1
4 DDD 2
5 EEE 2
6 FFF 1
7 GGG 2
8 HHH 2
9 III 1
I want query such as it get me 1 row from each category for each page in pagination
Now for 1st Page I need data as
id name catid
---------------------------------------------------------
1 AAA 1
4 DDD 2
Now for 2nd Page I need data as
id name catid
---------------------------------------------------------
2 BBB 1
5 EEE 2
Now for 3rd Page I need data as
id name catid
---------------------------------------------------------
3 CCC 1
7 GGG 2
and so on.
How can I achieve this.
SELECT * FROM table WHERE catid = 1 LIMIT :pageno 1
SELECT * FROM table WHERE catid = 2 LIMIT :pageno 1
I think, this will be simplest query to get required result.
This worked for me (MySQL 5.1 on Linux)
SELECT id, name, catid FROM
(SELECT id, name, catid FROM t WHERE catid=1 LIMIT 2,1) AS t1
UNION (SELECT id, name, catid FROM t WHERE catid=2 LIMIT 2,1)
ORDER BY catid
The 2 in the limit is the page number. Hence the output of this one is for the second page.
Yes, as Pradeep said, a LIMIT clause would be the simplest way to do it in MySQL. You'll still need a little bit of coding in PHP to produce clickable page numbers for your users.