MySQL query to find duplicate having more than (n) entries - mysql

I am having troubles with creating up this sql query to find records having more than (n) entries [n=1] in example
I have table
|--id-|--user_id--|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 2 |
| 6 | 3 |
want to retrieve duplicates in my table
|--id-|--user_id--|
| 2 | 1 |
| 3 | 1 |
| 5 | 2 |
any help is very much appreciated, thanks for reading though
UPDATE:
I am using Mysql v5.1

This would be my approach
SELECT ID, USER_ID
FROM TABLE
GROUP USER_ID
HAVING COUNT(1) > 1
MINUS
SELECT MIN(ID) ID, USER_ID
FROM TABLE
GROUP BY USER_ID

EDIT: oops, didn't see that you're using MySQL. you might be able to tweak this query to get it working in MySQL
not sure which version of SQL your using but here's the sqlserver answer:
SELECT * from [table_name] GROUP BY user_id HAVING COUNT(*) > n

Related

Random record from the table

I have customer table with 10 columns. In the table customer id is repeated. I need to take only one record every customer but randomly.
Let suppose customer table contain total 10000 records. But distinct customers is only 500.
So i need only 500 distinct customer data randomly.
I am using mysql 5.7.
Consider the following...
SELECT * FROM my_table;
+----+-------------+
| id | customer_id |
+----+-------------+
| 1 | 1 |
| 2 | 1 |
| 3 | 3 |
| 4 | 5 |
| 5 | 3 |
| 6 | 2 |
| 7 | 1 |
| 8 | 4 |
| 9 | 5 |
| 10 | 2 |
| 11 | 3 |
| 12 | 1 |
| 13 | 4 |
+----+-------------+
SELECT id
, customer_id
FROM
( SELECT id
, customer_id
, CASE WHEN #prev=customer_id THEN #i:=#i+1 ELSE #i:=1 END i
, #prev:=customer_id
FROM
( SELECT id
, customer_id
FROM my_table
ORDER
BY customer_id
, RAND()
) x
JOIN (SELECT #prev:=null,#i:=0) vars
) n
WHERE i = 1
ORDER
BY customer_id;
-- sample output, different each time --
+----+-------------+
| id | customer_id |
+----+-------------+
| 12 | 1 |
| 10 | 2 |
| 3 | 3 |
| 8 | 4 |
| 9 | 5 |
+----+-------------+
You do not want to ORDER BY RAND() because that will be extremely slow for a large table because it will actually sort all of those random records.
Instead pick a random int less than the number of rows in the table (random_num_less_than_row_count) and do this which is faster but not perfect.
SELECT * FROM atable LIMIT $random_num_less_than_row_count, 1
Or if u have a primary key that is an auto_increment you can pick a random int less than the highest id in the table (random_num_less_than_last_id) do the following which is pretty fast.
SELECT * FROM atable WHERE id >= $random_num_less_than_last_id ORDER BY id ASC LIMIT 1
I did a >= and an ORDER BY id ASC so that if you are missing ids you'll still get a result. But if you have many large gaps you need the slower first option above.
Not sure about it but it is a beginner level query which might to get the desired result
SELECT Distinct column FROM table
ORDER BY RAND()
LIMIT 500
PS: This code isn't in mysql 5.7. And if anyone have a better query more than happy to get corrected

MYSQL - Get all records that have more than 1 record for the same id

I apologize in advanced if I am not explaining this correctly. I can barely explain it in english terms, let alone in a mysql query.
I am trying to get the list of response_set_ids that have more than 1 record for a question_id.
This is an example of my data:
+----+-----------------+-------------+-----------+
| id | response_set_id | question_id | answer_id |
+----+-----------------+-------------+-----------+
| 1 | 10 | 1 | 4 |
| 2 | 10 | 2 | 5 |
| 3 | 10 | 3 | 6 |
| 4 | 10 | 3 | 7 |
| 5 | 11 | 1 | 8 |
| 6 | 11 | 2 | 9 |
| 7 | 11 | 3 | 10 |
+----+-----------------+-------------+-----------+
I would like to have a query that would return me a list response_set_ids, and in this particular example, I would expect to get returned 10 because that response_set has question_id -> 3 showing up more than once.
Please let me know if you need any further information to help me.
I have tried this:
select response_set_id, count(question_id) from responses group by response_set_id;
But that only gives me the counts of questions per response_set.
Thank you in advanced!
The simplest method doesn't use a subquery:
SELECT DISTINCT response_set_id
FROM responses
GROUP BY response_set_id, question_id
HAVING COUNT(*) > 1;
This is one of the very, very few instances where select distinct is used (appropriately) with group by.
select distinct response_set_id from (
select response_set_id , question_id
from
responses
group by
response_set_id, question_id
having count(*)>1) a
I believe this question has been asked before but I cannot add comments at my current rep:
SELECT DISTINCT response_set_id
FROM responses
GROUP BY question_id
HAVING COUNT(question_id) > 1

How to check which ID that has all the different values of another attribute?

I have a table like this:
+--------+--------+
| userID | itemID |
+--------+--------+
| 1 | 3 |
| 1 | 4 |
| 2 | 3 |
| 2 | 4 |
| 3 | 4 |
+--------+--------+
I am trying to select all the userID's that has all the different itemID's.
Meaning, if I were to expand itemID's in the future to have itemID 5 too, the same query would select all the userID's that has itemID 3, 4 and 5.
I've struggled with this problem for several hours now, but not managed to find the general query I am looking for.. I would appreciate all the help I could get!
Here is one method:
select userid
from t
group by userid
having count(distinct itemid) = (select count(distinct t2.itemid) from t t2);

Mysql needs to duplicate records using select query

I have a post table like following,
| ID | TITLE | NUMBER_OF_REPEAT
| 1 | post1 | 2
| 2 | post2 | 1
| 3 | post3 | 3
From the above table I need a select query to produce the row depending upon NUMBER_OF_REPEAT field.
Expected output,
| ID | TITLE
| 1 | post1
| 1 | post1
| 2 | post2
| 3 | post3
| 3 | post3
| 3 | post3
This kind of duplication should support the pagination also. Please help me.
Thanks in advance.
Something like this
SQL Fiddle
create table numbers (number int);
insert into numbers
select 1
union
select 2
union
select 3;
SELECT id, title
FROM tabl
JOIN Numbers
ON tabl.repeater >= Numbers.number
order by id
Its messy,but modify the numbers table for more repeats.
SQL Fiddle
I don't think this should do by DBMS. this can do in programe.
But you can create another table with only two rows and join with post table
let's say table dup with two rows.
SELECT ID ,TITLEfrom post, dup

mysql select ordernumber by group

I'm trying to do something like 'select groupwise maximum', but I'm looking for groupwise order number.
so with a table like this
briefs
----------
id_brief | id_case | date
1 | 1 | 06/07/2010
2 | 1 | 04/07/2010
3 | 1 | 03/07/2010
4 | 2 | 18/05/2010
5 | 2 | 17/05/2010
6 | 2 | 19/05/2010
I want a result like this
breifs result
----------
id_brief | id_case | dateOrder
1 | 1 | 3
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 2 | 1
6 | 2 | 3
I think I want to do something like described here MySQL - Get row number on select, but I don't know how I would reset the variable for each id_case.
This will give you how many records are there with this id_case value and a date less than or equal to this date value.
SELECT t1.id_brief,
t1.id_case,
COUNT(t2.*) AS dateOrder
FROM yourtable AS t1
LEFT JOIN yourtable AS t2 ON t2.id_case = t1.id_case AND t2.date <= t1.date
GROUP BY t1.id_brief
Mysql is permissive about columns which can be queries using GROUP BY. With a more stric DBMS you may need GROUP BY t1.id_brief, t1.id_case.
I strongly advise you to have the right indexes on the table:
CREATE INDEX filter1 ON yourtabl (id_case, date)