There is a table in my database duplicate_id which contains multiple ids and also contains many duplicate ids in it. What I have been trying to do is to sort top five ids which are being repeated the most in the table duplicate_id. Kindly let me know how can i do that
Table Structure: ID | Message
Expected output:
ID | Number of repeats
201 8
212 7
205 5
209 3
229 2
SELECT ID, COUNT(*) AS `Number of repeats`
FROM duplicate_id
GROUP BY ID
ORDER BY COUNT(*) DESC
LIMIT 5
Try the order by so sort your results:
Select * from table order by repeats desc limit 5
well... try this (I don't know mysql, so I have to guess)
select ID,
count(*) as 'Number of Repeats'
from duplicate_ID
group by ID
order by 2
Another approach would be
select ID, 'Number of Repeats'
from (
select ID,
count(*) as 'Number of Repeats'
from duplicate_ID
group by ID
) x
order by 'Number of Repeats'
Related
I have been searching at lot and have found topics like similar topics but none were exactly the solutions that I was looking for. In this case, I have a working code, but something that seems extremely hacky to me and should have a simpler and better way of getting it done.
The "Test" table
id
--
1
1
1
2
2
2
3
4
4
Nothing complex, just a simple table with some repeating id values
What I want is to group these id together and show all the id that are repeating the most i.e
id | count
----------
1 3
2 3
The solution that I have currently come up with
select
#max := max(count) as count
from (
select
id,
count(id) as count
from
test
group by
id
)
as
inner_table;
select
id, count
from (
select
id,
count(id) as count
from
test
group by
id
)
as
inner_table
where count = #max;
One way to do it with group by and having.
select id,count(*) as cnt
from t
group by id
having count(*)=(select count(*) as cnt
from t
group by id
order by cnt desc
limit 1)
The following does what it supposed to
it returns 200+ distinct records w/o the "limit 2"
What I want is to return 2 distinct records, but it stops after the 1st 2 records, meaning I only get 2 records
select distinct LEFT(`name`, LOCATE("(", `name`)-1), user_id, id
from ppbv79_listings
where user_id = 3798 and category_id = 30
group by LEFT(`name`, LOCATE("(", `name`)-1)
limit 2
Name user_id id
Germany 1213 Used Carl Sonnenschein 3798 2160555
Germany 1213 Used Carl Sonnenschein 3798 2160556
Try this:
select A.`trimmedName`, A.user_id, A.id
from
(select LEFT(`name`, LOCATE("(", `name`)-1)
`trimmedName`, user_id, id,count(category_id) `count`
from ppbv79_listings
where user_id = 3798 and category_id = 30
group by LEFT(`name`, LOCATE("(", `name`)-1), user_id, id
order by `count` desc) A
limit 2;
I assume there are some repetitions you would want to removed, and fetch the just the top 2 rows of data that repeats most.
I am trying to get users from mysql database if they have created a note with weather type = 'cloud','rain','clear' and not those who have missed even one out of these three.
eg.
id user_id weather_type
1 12 cloud
2 12 rain
3 12 clear
4 14 rain
5 15 clear
now here only user 12 have created notes with all three weather_type so only 12 should be fetched not 14 and 15.
Use group by and having:
select user_id
from mytable
where weather_type in ('cloud','rain','clear')
group by user_id
having count(distinct weather_type) = 3
You could use:
SELECT user_id
FROM tab
WHERE weather_type IN ('cloud','rain', 'clear')
GROUP BY user_id
HAVING SUM(weather_type='cloud') > 0
AND SUM(weather_type='rain') > 0
AND SUM(weather_type='clear') > 0;
SqlFiddleDemo
Output:
╔═════════╗
║ user_id ║
╠═════════╣
║ 12 ║
╚═════════╝
If you need all fields you could use:
SELECT *
FROM tab
WHERE user_id IN (prev_query);
Try this
SELECT user_id FROM tablex WHERE weather_type in ('cloud','rain','clear')
GROUP BY user_id
HAVING COUNT(DISTINCT weather_type) = 3
select user_id from table_name where weather_type in ('cloud','rain','clear')
group by user_id
having count(distinct weather_type) = 3
You can use group_concat function as below :
select user_id,weathers from
(select user_id, group_concat(weather_type)
weathers from tbl1 group by user_id
)t1
where find_in_set('cloud',weathers)>0 and
find_in_set('rain',weathers)>0 and
find_in_set('clear',weathers)>0 ;
I tested it and it works correctly.
More info :
A better and general solution for this kind of problem is :
Define another table named e.g. 'must_be' with two columns 'must_id' and 'weather' and fill it by favorite values like below:
must_id weather
1 cloud
2 rain
3 clear
Then use below query to fetch desired rows without hard code any value:
select user_id,count(*) cnt from
(select distinct user_id,weather_type from tbl1 join must_be on(weather_type=weather) ) t1
group by user_id having cnt=(select count(*) from must_be);
In the above query distinct keyword is necessary to get correct result in special situations.
After reading "trincot" response I decided to make my query better:
select user_id from tbl1 join must_be on(weather_type=weather)
group by user_id
having count(distinct weather_type)=(select count(*) from must_be);
I have the following table (user_record) with millions of rows like this:
no uid s
================
1 a 999
2 b 899
3 c 1234
4 a 1322
5 b 933
-----------------
The uid can be duplicate .What I need is to show the top ten records(need inclued uid and s) with no duplicate uid order by s (desc). I can do this by two steps in the following SQL statements:
SELECT distinct(uid) FROM user_record ORDER BY s DESC LIMIT 10
SELECT uid,s FROM user_record WHERE uid IN(Just Results)
I just wana know is there a bit more efficient way in one statement?
Any help is greatly appreciated.
ps:I also have following the SQL statement:
select * from(select uid,s from user_record order by s desc) as tb group by tb.uid order by tb.s desc limit 10
but it's slow
The simpliest would be by using MAX() to get the highest s for every uid and sorted it based on the highest s.
SELECT uid, MAX(s) max_s
FROM TableName
GROUP BY uid
ORDER BY max_s DESC
LIMIT 10
SQLFiddle Demo
The disadvantage of the query above is that it doesn't handles duplicates if for instance there are multiple uid that have the same s and turn out to be the highest value. If you want to get the highest value s with duplicate, you can do by calculating it on the subquery and joining the result on the original table.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT DISTINCT s
FROM TableName
ORDER BY s DESC
LIMIT 10
) b ON a.s = b.s
ORDER BY s DESC
I'm currently using PHP/MySQL to select some data from my database. My current SELECT statement is:
mysql_query("SELECT * FROM tblpm WHERE receiver_id ='$usrID' GROUP BY
thread_id ORDER BY unique_id DESC") or die(mysql_error());
There are some columns that have the same "thread-id", thus, I am using the GROUP BY, so that only one "thread-id" column is selected. Everything works fine with the grouping, however, it is selecting the first 'thread-id' column, and not the most recent (which is what I want).
For example, there are 4 columns all with the same thread_id:
thread_id unique_id
222 1
222 2
222 3
222 4
When I do the GROUP BY, I would like it to retrieve the one with unique id 4 (the most recently created column), not unique id 1 (the oldest column created).
Any ideas on how to achieve this with my SELECT statement? Thanks.
SELECT thread_id, MAX(unique_id)
FROM tblpm
GROUP by thread_id
so:
mysql_query(<<<END
SELECT thread_id, MAX(unique_id)
FROM tblpm
WHERE receiver_id ='$usrID'
GROUP by thread_id
END
) or die(mysql_error());
and of course make sure you escape $usrID if it comes from the browser.
SELECT DISTINCT thread_id, unique_id
FROM tblpm
WHERE receiver_id = '$usrID'
GROUP BY thread_id
ORDER BY unique_id DESC;