Counting the number of pattern occurrences in a certain column with MySQL - 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%'

Related

Get a table that groups by value and provides two columns containing each a different count for that value

I have a table like the below
Pet Name
Test
Result
Celine
A
Positive
Diogo
A
Negative
Jordah
A
Positive
Diogo
B
Positive
Jordah
B
Negative
Caesar
A
Negative
I need to come up with a query (or a view) showing the below
Pet Name
Total tests
Positive
Diogo
2
1
Jordah
2
1
Celine
1
1
Caesar
1
0
I tried a query like
SELECT Pet Name, COUNT(*)
WHERE Result=‘Positive’
GROUP BY Pet Name
ORDER BY COUNT(*) DESC
which is ok, but it doesn’t provide me with the number of tests done by each pet.
The CASE WHEN sort of options do not seem to be applicable as the table gets populated with more Pet Names as vet makes new friends.
Thanks in advance.
This has been identified as duplicate of this, but I don't think it's the case. As I mentioned in above, it's the actual query that will provide the pet name, I won't know that name in advance, so no conditions such as IF or CASE WHEN sounds like helpful.

Select and display by a value of a number

I want to select from my database and to display each value by a value of a number, example:
I have in database this:
Peter 3.80
Maria 5.67
John 2.52
Robert 1.53
I want to display this values but I want to display by example Maria show more times than Robert because she have value of number 5.67 and Robert have 1.53
Output must be Names like Maria, John...
At every refresh show me one name but I need frequency with value number high
I think you are looking for order by. Just use order by col_name, here you may wanna add desc for descending order
Are you searching for sorting the name by the value of number ?
You can try use
SELECT name
FROM your table name
ORDER BY your column name DESC;
You can see reference here to for this Mysql Sort

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%'

Use MySQL to Find Duplicates and Display on One Line

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

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.