Use MySQL to Find Duplicates and Display on One Line - mysql

I'd like to use MySQL to find duplicates and then display those items as one, combined record. For example, I have two columns: Name and Fruit. A sampling of records might look like this:
Joe - Peaches
Faye - Bananas
Joe - Starfruit
Sam - Apples
I want to display this data as:
Joe - Peaches, Starfruit
Faye - Bananas
Sam - Apples
Is that possible? Could you help me with the MySQL query to start? I'm using VB.NET for my application. Thank you.

Use GROUP_CONCAT for that.
SELECT personName, GROUP_CONCAT(fruitName) fruitList
FROM tableName
GROUP BY personName

You'll want to use group_concat here.
SELECT name, group_concat(fruit)
FROM table
GROUP BY name

Related

Search words in any order using "Like" query

I have a students table with following rows in it.
ID
Name
1
John Smith
2
Hellen Fernandis
3
Ali Abbas
4
Sameer Khan
I want that even if I use the below mentioned query
Select * from students where name like '%Abbas Ali%'
Row No. 3 should come in result.
You can't do it in a single LIKE, you need multiple conditions:
WHERE name LIKE '%Abbas%' AND name LIKE '%Ali%'
If you to do it in other manner, just try this:
SELECT * FROM students WHERE name LIKE '%Abbas' AND name LIKE 'Ali%'
Select * from students where name like '%Abbas%Ali%'

SQL : Having with calculation on some parameters

I'm currently looking for a way of concidering the value of the difference between some parameters in a HAVING clause.
I explain,
I have a table like :
MyTable
id name date
1 john 7/12
2 doe 7/12
3 john 7/14
4 john 9/13
5 doe 9/14
and i want to make a group by like
GROUP BY name
HAVING 'MyTAble[a].date - MyTAble[b].date > 1 month'
to have (count) :
john (3)
doe (1)
john (1)
doe (1)
I know there is a MONTH() function but if I make a group by name, MONTH(date) I will not have the expected result.
I probably miss an obvious solution (or it's just impossible). Anyway, hope you have an idea.
Lacking more information about your query or database engine, I would say that the answer is to use the where clause rather than group by / having. In my experience, having is used to limit query results based on the value of an aggregate function, which your having clause doesn't use.
Ok,
I guess it's a little bit far-fetched but I found a solution, so I worked on the select clause :
SELECT mytable.name name, mytable.date , CONCAT(mytable.name,DAY(mytable.date)) group_id
I create an id (group_id) that is common to all the entities I want to gather, then I GROUP BY this common id

Counting the number of pattern occurrences in a certain column with MySQL

I want to search the column Name of a certain table to see how many rows have a Name value that matches a certain pattern. For example, if the pattern I am looking for is %Peter% and in this table there are 5 rows with the Name values:
Peter
Peter Smith
George Peter
Peter Peter
Carl
I want to obtain the value 4. I tried to use COUNT, but don't know how to combine it for example with LIKE. How do I go about to do this?
Not sure why didn't LIKE work for you, but this SHOULD do the trick:
SELECT COUNT(*) FROM table
WHERE name LIKE '%Peter%'

mysql combining records from one table

I have a single table that uses test# as the primary key. Here is what that table looks like:
Test# Name VerbalScore readingScore Notes
1 Bobby 92 Good job
2 Bobby 40 You Suck Bobby
The problem is I want to view and be able to see when there are multiple verbal scores for the same Name (so be able to see if the person took the same test more than once).
I want to have some kind of select statement to get this result from the above table:
1 Bobby 92 40 Good job, You Suck Bobby
Is that possible?
I am not totally sure I understand what you mean by "see when there are multiple verbal scores" but with mysql 5+, try
SELECT
Name,
GROUP_CONCAT(VerbalScore),
GROUP_CONCAT(readingScore),
GROUP_CONCAT(Notes)
FROM
myTable
GROUP BY
Name;
GROUP_CONCAT is a mysql specific grouping function.

Selecting data from two tables

Hello everybody
Well my question is about sql commands...
If I have 2 tables with the same number of columns and the same fieldnames (e.g: A(n,name,date) and B(n,name,date))
In the website, I want to retrieve data from both tables and display them in order by date descendent.
(The use of two tables is due to difference in tables database or server,or just the use of every table.. sometimes there's a need to display both tables in one order)
exemple
table Sport_news(N_event,Title,Texte,Date)
table International_news(N_event,Title,Texte,Date)
Display:
Christiano Ronaldo ... 2011/25/01
christiano ronaldo is one of the famous...
Barack Obama president of the USA... 2011/24/01
Barak obama........
The arsenal has... 2011/23/01
Chamakh, player of arsenal is anger.....
I hope that the idea is clear : and thank you!
You want UNION
select a.name,a.date
from table1 a
where ...
UNION ALL
select b.name,b.date
from table2 b
where ...
order by 2 desc
When you use a UNION, you specify the order by with column numbers instead of names.