I'm using MySQL 5.7.12. I have a simple query that looks like the following:
SELECT COUNT(id) as count, pic_id
FROM user_pictures
GROUP BY pic_id
I would like to exclude all the duplicate USER_ID records from grouping. So I did the following, however, it's not returning any result:
SELECT COUNT(id) as count, pic_id
FROM user_pictures
GROUP BY pic_id
HAVING count(user_id) = 1
What am I missing here?
Use DISTINCT so it doesn't count duplicates of the same user_id:
SELECT COUNT(id) as count, pic_id
FROM user_pictures
GROUP BY pic_id
HAVING count(DISTINCT user_id) = 1
Related
bid_count is returning 1 ... how do I get it to return the correct bid count? I know that it's not returning the right count because I'm grouping by user_id. Can I alter the query so it counts also.
"SELECT id, bid, item_id, user_id, MAX(bid) AS max_bid, COUNT(bid) AS bid_count, bid_date
FROM bids
WHERE item_id = ?
GROUP BY user_id ORDER BY id DESC"
You shouldn't use unaggregated expressions in GROUP BY unless they are all same within the group.
If you want to return the record holding the counts by user_id, you should use an subquery to get the counts then join them to the result:
SELECT id, bid, item_id, user_id, bid_date, max_bid, bid_count
FROM bids
LEFT JOIN
(SELECT user_id,MAX(bid) AS max_bid, COUNT(bid) AS bid_count
FROM bids
WHERE item_id = ?
GROUP BY user_id ) as group_table
ON bids.user_id = group_table.user_id
WHERE bids.item_id = ?
Try grouping it based on item_id. item_id should the one that has different bids.
I have a table tbl_user and I want to find which user_name(s) have the most repeating values in the table. I only want to retrieve those user_id(s).
I also need a separate query to find the count the of duplicate user_name(s) in the table .
First query to find the biggest amount of repeated usernames:
SELECT user_name, COUNT(user_id) AS amount
FROM tbl_user
GROUP BY user_name
ORDER BY amount DESC
And then grab id`s:
# faster way using username from last query
SELECT user_id
FROM tbl_user
WHERE user_name = '$user_name'
# longer way
SELECT user_id
FROM tbl_user
WHERE user_name = (
SELECT user_name
FROM tbl_user
GROUP BY user_name
ORDER BY COUNT(amount) DESC
LIMIT 1
)
To find the count of duplicates:
SELECT count(id) AS num_duplicates FROM tbl_users GROUP BY user_name ORDER BY num_duplicates DESC
To find the user_id's of the most duplicated user_name:
SELECT user_id FROM tbl_user
WHERE username IN ( SELECT user_id
FROM tbl_users
GROUP BY user_name
ORDER BY count(id) DESC LIMIT 0, 1)
SELECT COUNT(*) FROM tbl_user
GROUP BY tbl_user.user_name
Will give you the number of duplicate user_names. You can use a scripting language to select out the highest one if you like, or just look at the list if it's not something you need automated.
If you mean you want to count the occurences of a particular username, something like this may work for you.
select user_name,user_id,count(*) as mycount from tbl_user group by user_name order by mycount desc limit 5
This should get the top 5 entries
You could select the user_names which are duplicated with this SQL:
SELECT COUNT(1) AS duplicates, user_name
FROM tbl_user
GROUP BY user_name
HAVING duplicates > 1
Having done this, you can then use it as a subquery to find out the count of duplicate user_names
SELECT COUNT(1) AS count_of_user_names, SUM(duplicates) AS duplicate_records
FROM (
SELECT COUNT(1) AS duplicates, user_name
FROM tbl_user
GROUP BY user_name
HAVING duplicates > 1
) AS subquery
Or use it to find out the user ids:
SELECT user_id
FROM tbl_user
WHERE user_name IN (
SELECT user_name
FROM tbl_user
GROUP BY user_name
HAVING COUNT(1) > 1
)
I have this request :
SELECT id_user
FROM posts
GROUP BY id_user
ORDER BY COUNT(*) DESC
...which will return the id_user, ordered by their number of occurrence in the posts table.
But along with the id_user information, I would like to keep track of the COUNT(*) and store it somewhere, but I have no idea how to do it.
Use:
SELECT id_user,
COUNT(*) AS numPosts
FROM posts
GROUP BY id_user
ORDER BY COUNT(*) DESC
The column alias in the example, numPosts, can then be referenced in whatever you're already using to get the id_user column values.
you can do:
select id_user, count(*) total_count
FROM posts
GROUP BY id_user
ORDER BY COUNT(*) DESC
That way you can still retrieve the user id and the total times it appeared in the table
I have the following table structure in my db (MySQL):
id group_id item_id project_id user_id
Users can have multiple entries withing the same project. How do I count unique users withing a particular project (minus project owner id)?
SELECT COUNT(user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3
GROUP BY user_id
This looks right but I don't believe I'm getting the right results. Am I missing something?
Select Count(Distinct user_id)
From MyTable
Where project_id = $myProject
And user_id != 3
Add DISTINCT to your COUNT and eliminate the GROUP BY.
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3
You don't need a GROUP BY clause for this.
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
WHERE project_id = $myProject
AND user_id != 3;
If you want to list the member count for each group in the same query, you can GROUP BY project_id:
SELECT COUNT(DISTINCT user_id) AS cnt
FROM myTable
GROUP BY project_id;
By grouping on user_id as you do now, every row in the resultset will contain 1.
Try Distinct?
SELECT DISTINCT COUNT(user_id) AS cnt FROM myTable WHERE project_id = $myProject AND user_id != 3
I have the following MySql table (user_actions) with the following columns:
id, user_id, created_at, action_type
I want a sql query that will get the latest actions that the user performed with no duplication of the actions.
for example:
user_id 1 has 3 records that has the action_type "follow"
and 2 records that has the action_type "unfollow"
in this case i want the query to return two records, one with action_type "follow" and one with "unfollow"
any thoughts?
You can use SQL's group by clause for this:
select user_id, action_type, max(created_at)
from user_actions
group by user_id, action_type
try this:
select ua.*
from user_actions ua
join (select max(id) as max_id,user_id,action_type from user_action group by user_id,user_action) ua_max
on ua.id=ua_max.max_id and ua.user_id=ua_max.user_id and ua.action_type=ua_max.action_type
Try this query:
select * from user_actions where action_type, created_at in
(select action_type, max(created_at) from user_actions group by action_type);