Find similar name in view based on other table - mysql

I have one view from my database contains a lot of customer name and address
ID Name Address Event
A1 Jon California Hockey
B3 Mark Texas Marathon
B4 marc Texas Marathon
A5 Jacobus Florida Soccer
C2 BILLy ALASKA Basket
and then one table master customer
Name
JOHN
MARK
JACOB
BILLY
I want to remove similar/duplicate name based on same address and event and repair misspell name automatically based on same characteristic then insert it into new table called true_customer so teh resut will be like this
ID Name
A1 JOHN
B3 MARK
A5 JACOB
C2 BILLY
Note:
I can't use function like levenshtein etc this case

Related

Finding the Max of multiple score assigned to a student ID number

I am currently a teacher and I have a database containing hundreds of students. That database has student ID numbers, student names, and scale scores for reading and math tests as shown below. I want column F to display the student's highest score and column G to display when student tested recently. I want these columns to display these data by linking them through their ID numbers. I could do that if the ID numbers were not repeated but having the same ID numbers being repeated multiple times is causing me problems.
column A column B column C column D column E column F column G
ID number last name First name Math score test date Highest score Most recent test date
1705555 Smith Joe 543 9/1/19 ? ?
1706666 Solis Juan 459 10/8/19
1773333 Jackson Devonte 654 10/14/19
1772222 Villa Maria 329 9/4/19
1778888 Bilal Issa 600 10/21/19
1705555 Smith Joe 410 8/11/19
1706666 Solis Juan 389 7/25/19
1773333 Jackson Devonte 500 8/24/19
1705555 Smith Joe 510 6/30/19

Query for finding similar interests together

Name Place visited
Ash New york
Bob New york
Ash Chicago
Bob Chicago
Carl Chicago
Carl Detroit
Dan Detroit
Above is the sample table. The output should be two names who visited place together. I.e. the output should be Ash and Bob since the places visited by Ash also visited by Bob.
Output:
Name1 Name2
Ash Bob
What is a query for this using MySQL or even relational algebra?
The simplest method is to use group_concat(). Assuming no duplicates,
select places, group_concat(names) as names
from (select name, group_concat(place order by place) as places
from t
group by name
) t
group by places
having count(*) > 1;
This will return all the names with exactly the same places on a single row. The names will be in a comma-delimited list.

Primary Key and Functional Depe

I have a table below and I would like to get confirmation on one of the functional dependencies.
Taylor Johnson is the name of two twins who live in the same house.
Student ID is a unique ID and hence it is the candidate/primary key.
But Student ID doesn't functionally determine Student Name or does it? I don't think it does because knowing student ID wouldn't tell you which Taylor Johnson you are talking about. If that is the case, then I cannot say that my candidate/primary key (Student ID) functionally determines all other attributes in the table even though it is a unique key. Doesn't it then violate the definition of a primary key? Thanks for any insights in advance!
Student ID Student Name DOB Street City State Zip
1 Taylor Johnson 4/10/1988 Meadow Raleigh NC 27606
2 Taylor Johnson 4/10/1988 Meadow Raleigh NC 27606
3 Kim Blake 2/10/1998 Crescent Cary NC 27512

Merging and converting rows to columns

I have a list of data that happens to have been set up like this:
entryID fieldID Value
100 1 John
100 2 Smith
100 3 USA
101 1 Jane
101 2 Doe
101 3 USA
The way it works is, when a "ticket" is created, it assigns an entryID for all the data in that ticket, but each field also has it's own fieldID. So first name is always a fieldID of 1, last name is 2, etc.
What I need to be able to do is create a view that will look like this:
First Name Last Name Country
John Smith USA
Jane Doe USA
I need to be able to do this in MySQL, and not SQL or excel, as some other similar problems have addressed.
I just found the answer to my own question, so I will post it here for future users.
This website explains how to do exactly what I was asking for: http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/

IF EXISTS in WHERE clause?

I want to insert a condition in one of my queries, but don't really know how to do it.
Basically, I'm sorting a bunch of files by their folder _id, i.e. the folder they belong to (using order by folder_id). What I'd like now, for each of these groups of files, is the query to pick the first file that satisfies a particular condition. If that condition is not satisfied by any of the files in a folder, than the query should pick the first document available in the folder.
This is my query (simplified):
select folder_id,
file_id,
first_name
from f_file
where first_name = 'John'
order by folder_id
So, if my results now are:
FOLDER ID FILE ID AUTHOR
A 32 John
A 41 John
A 56 Thomas
A 78 Peter
B 02 Peter
B 03 Peter
B 45 Peter
C 34 John
C 56 Thomas
C 77 Peter
C 86 John
D 12 Peter
D 34 Thomas
D 89 Thomas
for each folder, I'd like the query to show only the first document (lowest file_id) that was created by John. If there are no documents created by John at all in that folder, than the first one from any other author will do.
FOLDER ID FILE ID AUTHOR
A 32 John
B 02 Peter
C 34 John
D 12 Peter
So, the ID has to be the lowest, but the Author has to be John or another one.
My idea would be to insert an IF EXISTS clause in the WHERE clause, but how to do it?
Thanks for your responses.
Val