Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Select rows with more than one occurrence

Tags:

mysql

Here's my query. It selects a list of ids from two tables across two databases. The query works fine.

select en.id, fp.blogid from french.blog_pics fp, french.blog_news fn, english.blog_news en  where fp.blogid = fn.id  and en.title_fr = fn.title  and fp.title != ''  

I only want to display rows where a en.id occurs more than once

So for instance, if this was the current query result

en.id fp.blogid ---------------   10     12   12     8   17     9   12     8 

I only want to query to show this instead

 en.id fp.blogid occurrences  -----------------------------   12     8           2 
like image 231
HyderA Avatar asked Nov 22 '10 06:11

HyderA


People also ask

How do I select multiple rows in MySQL?

To select last two rows, use ORDER BY DESC LIMIT 2.

How do I get more than one record in SQL?

If you want to insert more rows than that, you should consider using multiple INSERT statements, BULK INSERT or a derived table. Note that this INSERT multiple rows syntax is only supported in SQL Server 2008 or later. To insert multiple rows returned from a SELECT statement, you use the INSERT INTO SELECT statement.

How do I have multiple rows in one row in SQL?

You can concatenate rows into single string using COALESCE method. This COALESCE method can be used in SQL Server version 2008 and higher. All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.


1 Answers

select en.id, fp.blogid, count(*) as occurrences from french.blog_pics fp, french.blog_news fn, english.blog_news en  where fp.blogid = fn.id  and en.title_fr = fn.title  and fp.title != '' group by en.id having count(*) > 1 
like image 62
Stefan Mai Avatar answered Sep 22 '22 16:09

Stefan Mai