I need to a MySQL query that returns the results of a table where the elements of a specific column are identical in at least 'n' records.
example pseudocode:
SELECT * FROM Table WHERE col has at least 3 identical
select * from table where col in (
select col from table group by col having count(*) > 3
)
SELECT col, COUNT(*) AS total FROM tbl GROUP BY col HAVING total >= 3
select * from table a
inner join (
select col,n=count(*)
from table
group by col
having count(*) >= 3 ) as b
on (a.col=b.col)
mysql allows you to write something like the following:
select *
from table
group by col
having count(*) >= 3
other dbmss force you to specify all columns explicitly and use MAX or MIN on them
SELECT DISTINCT * FROM TableName t WHERE 2 < (SELECT COUNT(*) FROM TableName p WHERE p.col = t.col)
Related
I have mysql table like this
I want to get row that has minimum 2 or more than 2 (multiple) row only from this table, so the result would be like this
What do i do?
thank you
Use GROUP BY and HAVING clauses
SELECT t.* FROM my_table t
JOIN (
SELECT cust_id, MIN(transaction_no) AS transaction_no
FROM my_table
GROUP BY cust_id
HAVING COUNT(cust_id) > 1
) agg ON t.transaction_no = agg.transaction_no
I have a table having id and no field, what I really want is the result raw will be repeated no filed times, if the no field is 2 then that raw must be repeated twice in result.
this is my sample table structure:
id no
1 3
2 2
3 1
now I need to get a result like:
1 3
1 3
1 3
2 2
2 2
3 1
I tried to write mysql query to get the result like above, but failed.
You need a table of numbers to accomplish this. For just three values, this is easy:
select t.id, t.no
from t join
(select 1 as n union all select 2 union all select 3
) n
on t.no <= n.no;
This query must do what you want to achieve:
select t.id, t.no from test t cross join test y where t.id>=y.id
not completely solve your problem, but this one can help
set #i=0;
select
test_table.*
from
test_table
join
(select
#i:=#i+1 as i
from
any_table_with_number_of_rows_greater_than_max_no_of_test_table
where
#i < (select max(no) from test_table)) tmp on no >= i
order by
id desc
EDIT :
This is on SQL Server. I checked online and see that CTEs work on MySQL too. Just couldn't get them to work on SQLFiddle
Try this, remove unwanted columns
create table #temp (id int, no int)
insert into #temp values (1, 2),(2, 3),(3, 5)
select * from #temp
;with cte as
(
select id, no, no-1 nom from #temp
union all
select c.id, c.no, c.nom-1 from cte c inner join #temp t on t.id = c.id and c.nom < t.no and c.nom > 0
)
select * from cte order by 1
drop table #temp
Hi I want to GROUP BY file_serial but only when file_serial > 1 that mean no duplicate file_serial with value bigger than 1
so it should look like
Any idea how, would be great.
Thanks.
One way would be
select * from table
where file_serial > 1
group by file_title
having count(*) > 1
UNION
select * from table
UPDATE
The above will work only if all the selecting columns with the same value. In this case you may need to change as since file_id is different for the same title.
select * from table
where file_serial > 1
group by file_title
having count(*) > 0
UNION
select * from table
file_serial <= 1
DEMO
Try below query, where no need of extra having clause-
SELECT
file_id,file_title,file_serial
FROM mytable
WHERE file_serial<=1
UNION ALL
SELECT
file_id,file_title,file_serial
FROM mytable
WHERE file_serial>1
GROUP BY file_serial;
I currently have the code below and it works for getting me the 2 smallest number, but I want to get all of the 2nd smallest numbers and link them to their name as opposed to just one of them. lets say the numbers in the tables was made up of this:
Name| number
----|------
w 2
a 8
s 2
e 2
z 3
I would want to get
w 2
s 2
e 2
and now I am just getting w 2
SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table);
If this code gets you the second smallest number (what you want):
SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table);
Then simply do:
select *
from table
where col = (SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table));
I didn't understand well, but if you're using LIMIT 1,1, you will only get 1 row or none.
Just use what #491243 commented on your question.
SELECT * FROM tablename WHERE number = (SELECT MIN(number) FROM tableName);
Forget it, now I understood the question.
Try this:
SELECT * FROM tablename WHERE number =
(SELECT number FROM tablename WHERE number !=
(SELECT MIN(number) FROM tablename) ORDER BY number LIMIT 1);
Hope this helps.
EDIT: Using the SQLFiddle table:
SELECT * FROM ships WHERE gunsize = (
SELECT gunsize FROM ships WHERE gunsize !=
(SELECT MIN(gunsize) FROM ships) ORDER BY gunsize LIMIT 1);
http://sqlfiddle.com/#!2/9ca94/11
SELECT name,(SELECT MAX(gunsize) FROM ships s2
WHERE s2.Name=s.name and gunsize not in (select max(gunsize) from ships))as gunsizes
FROM ships s
Something like this?
I have a table with the following columns:
Categorie
Rubriek
Adv_nr
For each rubriek it is possible that there are 100 or more adv_nr's.
I want to select max 5 rows from each rubriek, is this possible in one query?
select * from table as t1
where (select count(*) from table as t2
where t1.rubriek = t2.rubriek and t2.adv_nr > t1.adv_nr) < 5
order by rubriek,adv_nr desc
select * from *ad_table*
where category_id IN (
select *category_table*.id
from *category_table*, *group_table*
where *category_table*.id = *group_table*.category_id)
LIMIT 5;