MySQL selecting distinct names from two columns - mysql

I have two columns of coaches names:
coach1
JOHN
JACOB
MARY
coach2
JOHN
JACOB
HENRY
I would like to select all DISTINCT values between the two columns.
So that my SELECT statement will read,
JOHN
JACOB
MARY
HENRY
with no duplicates. Any suggestions as to the most efficient way to do this?

SELECT COACH1 AS NAME FROM TABLE
UNION
SELECT COACH2 AS NAME FROM TABLE
Is a way to do. I'm not saying it's the "most" efficient to do, though :) Shouldn't be too bad. UNION by default will select only distinct values.

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

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

How to select all where column data begins with certain numbers in MySQL

This is my table:
studentID lastName firstName
08422567 Brown Susan
10356844 Black Roger
10659475 White Steven
09463729 Grey Naomi
I am trying to select all students whose ID begins with 10, so basically Roger and Steven.
Here are examples of what I was trying:
SELECT * FROM student WHERE studentID='10356844' AND studentID='10659475'
SELECT * FROM student WHERE studentID='10356844,10659475'
Would I need to use regular expressions for this, surely there must be a simpler way to run a query like this? In the future there may be 100 number beginning with 10, I cannot add all of these to a query, and the difficulty of this is why I ask this question here, in order to get the correct solution.
Not a regular expression, but WHERE studentID LIKE '10%'. The % is a wildcard, so it will match any student whose ID begins with 10.
This would never succeed:
SELECT * FROM student WHERE studentID='10356844' AND studentID='10659475'
because there is no row that has both of those values of studentID. It's impossible. What you meant to do was:
SELECT * FROM student WHERE studentID='10356844' OR studentID='10659475'

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

find out count of comma based value in MySql

I have two tables.
Table Emp
id name
1 Ajay
2 Amol
3 Sanjay
4 Vijay
Table Sports
Sport_name Played by
Cricket ^2^,^3^,^4^
Football ^1^,^3^
Vollyball ^4^,^1^
Now I want to write a query which will give me output like
name No_of_sports_played
Ajay 2
Amol 1
Sanjay 2
Vijay 2
So what will be Mysql query for this?
I agree with the above answers/comments that you are not using a database for what a database is for, but here is how you could calculate your table from your current structure in case you have no control over that:
SELECT Emp.name, IF(Played_by IS NULL,0,COUNT(*)) as Num_Sports
FROM Emp
LEFT JOIN Sports
ON Sports.Played_by RLIKE CONCAT('[[:<:]]',Emp.id,'[[:>:]]')
GROUP BY Emp.name;
See it in action here.
UPDATE: added the IF(Played_by IS NULL,0,COUNT(*)) instead of COUNT(*). This means that if an employee doesn't play anything they'll have a 0 as their Num_Sports. See it here (I also added in those ^ characters and it still works.
What it does is joins the Emp table to the Sports table if it can find the Emp.id in the corresponding Played_by column.
For example, if we wanted to see what sports Ajay played (id=1), we could do:
SELECT *
FROM Emp, Sports
WHERE Sports.Played_by LIKE '%1%'
AND Emp.id=1;
The query I gave as my solution is basically the query above, with a GROUP BY Emp.name to perform it for each employee.
The one modification is the use of RLIKE instead of LIKE.
I use RLIKE '[[:<:]]employeeid[[:>:]]' instead of LIKE '%employeeid%. The [[:<:]] symbols just mean "make sure the employeeid you match is a whole word".
This prevents (e.g.) Emp.id 1 matching the 1 in the Played_by of 3,4,11,2.
You do not want to store your relationships in a column like that. Create this table:
CREATE TABLE player_sports (player_id INTEGER NOT NULL, sport_id INTEGER NOT NULL, PRIMARY KEY(player_id, sport_id));
This assumes you have an id column in your sports table. So now a player will have one record in player_sports for each sport they play.
Your final query will be:
SELECT p.name, COUNT(ps.player_id)
FROM players p, player_sports ps
WHERE ps.player_id = p.id
GROUP BY p.name;