I have a table where there is a full name column. I want to get either the unique given name or given names sorted by how many times they occur.
fullname
-----------
Barack Hussein Obama
Michael Jackson
William Jefferson Blythe
Michael Bloomberg
so the output will be either
Barack Hussein Obama
William Jefferson Blythe
Or
Barack Hussein Obama
William Jefferson Blythe
Michael Jackson
Michael Bloomberg
Or
1|Barack
1|William
2|Michael
Something like that. My aim is to see the foreign students in my database. I only have their full name to make a guess.
You can use SUBSTRING_INDEX(fullname,' ',1) to extract the "given name" as per your definition.
You can then use this for grouping or sorting as you seem fit, e.g.
SELECT COUNT(*),SUBSTRING_INDEX(fullname,' ',1) AS givenname
FROM yourtable
GROUP BY givenname;
Related
Let's say I've got a model:
class Person(models.Model):
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
I'd like, by searching the first letter of a first_name, to get a list of Person. But each one must have a different last_name.
For example:
searching M... in the list of:
Frank Edmond
Marc Thomas
Matthew Ronald
Matthew Smith
Matthew Thomas
Richard Thomas
will give you
Marc Thomas
Matthew Ronald
Matthew Smith
In short, how to translate in MySQL:
Person.objects.filter(first_name__startswith='M').distinct('last_name')
In MySQL
SELECT DISTINCT <table>.last_name FROM <table> WHERE <table>.first_name LIKE 'M%'
Replace <table> with your table name.
My Table column "business" looks like this:
Michael Kors
Baltimore Michael Kors
Charlotte Michael Kors
Michael Texas
Kors Dallas
Michael Kors
I have to apply order by on this column on String 'Michael Kors', so the sorted result should be something like this:
Michael Kors
Michael Kors
Baltimore Michael Kors
Charlotte Michael Kors
Kors Dallas
Michael Texas
if String contains substring Michael Kors it should be on top in alphabetical order. So in the example above, 2 rows with exact match is on top and after that Baltimore and Charlotte is 3rd and 4th in alphabetical order. Not worried about other strings which does not contain the exact word Michael Kors
I tried using Substring_Index but looks like it doesn't works well with substring with spaces. All help appreciated.
You can have multiple levels of ordering:
order by
locate('Michael Kors', business)=1 desc,
locate('Michael Kors', business)>0 desc,
business
The first one sorts the exact matches to top, next level sort the rest of the matching rows and the third sorts all the rest.
Try this one,
order by FIELD(business, 'Michael Kors’)
You could list Boolean expressions in your order by clause, and apply a descending order so that records for which this expression is true, will be ordered before those that yield false. Then specify an alphabetical order at the end to determine the order when all other expressions give no distinction for two records:
select *
from mytable
order by (business = 'Michael Kors') desc,
(business like '%Michael Kors%') desc,
(business like '%Kors%') desc,
(business like '%Michael%') desc,
business
I have the following table:
First Name
Bryce
Marcellin
Caroline
Kerry
Roberto
Mary
Carol
Warren
Bonnie
Terry
Louis
Michelle
Bobby
Tony
Vic
Frank
Roberto
Jose
Doug
Brian
William
Aiden
Davis
What exactly does SELECT FirstName FROM Members WHERE FirstName > "Maria"; search for ? in particular, the WHERE statement.
It returns the names:
Roberto, Mary, Warren, Terry, Michelle, Tony, Vic, Roberto and William
I thought it was searching for FirstName strings that are longer than 5 characters but this is not the case since Tony and Vic are also returned.
It searches for terms that are in alphabetical order AFTER "Maria."
For example, with "Jack, James, Jim" if you searched for SELECT name FROM table WHERE first_name >= 'James' you would receive the results of 'James' and 'Jim', since alphabetically those two are after one another. The reason Vic, Robert, William, etc are returned is because they are alphabetically after the value of "Maria"
Maria's string length is 5 characters, so use length function to find length of individual names and check it to be greater than 5 as below:
SELECT FirstName
FROM Members
WHERE length(FirstName) > 5;
Or if your name is going to be dynamic, you could use like:
SELECT FirstName
FROM Members
WHERE length(FirstName) > length("Maria");
If you need all names that comes after Maria and length greater than 5 then use:
SELECT FirstName
FROM Members
WHERE FirstName > 'Maria';
AND length(FirstName) > length('Maria');
I want to sort my entries in alphabetical order based on two fields First and Last.
Both fields have the domain of CHAR(30). Neither of the fields is a key of any kind.
For an example I am using the following table.
Last First
Banner Bruce
Wayne Bruce
Wayne Benjamin
Kent Clark
Rodgers Steve
Jordan Hal
Stark Tony
Howlett Logan
I then use the following SQL Statement which according to here (Look at the last example) Is a valid SQL Statement that should sort by Last name then a sub-sort of the First names.
SELECT * FROM Heros ORDER BY Last ASC, First ASC;
The results I am looking for are:
Last First
Banner Bruce
Howlett Logan
Jordan Hal
Kent Clark
Rodgers Steve
Stark Tony
Wayne Benjamin
Wayne Bruce
Instead I get:
Last First
Banner Bruce
Howlett Logan
Jordan Hal
Kent Clark
Rodgers Steve
Stark Tony
Wayne Bruce
Wayne Benjamin
Basically the last two rows are not sorted by First name after they were sorted by their Last name. I am not sure what is why the rows returned are not ordered as I had expected. Besides SQlCommands.net I looked at dev.mysql.com. Several Stack Overflow topics had suggested answers where the SQL Statement was almost identical to this syntax. Any thoughts on what I am missing would be very appreciated.
If there are space around First or Last name then try Trim() function in ORDER BY clause like this:
SELECT * FROM Heros
ORDER BY TRIM(Last) ASC
, TRIM(First) ASC;
See this SQLFiddle
After doing some research I need some advice. This is a small project which allows teachers to post assignments online, and students to view/submit those assignments.
Students can have multiple courses assigned to them, so my question is: What is the best way to set up a table?
Example 1:
student courses
John Smith Math, Chemistry, English
Mary White Math, Biology
Example 2:
course students
Math John Smith, Mary White
Biology Mary White
Chemistry John Smith
English John Smith
I apologize in advance if this is a noobie question, but I want to avoid doing things wrong from the get-go and I'm open to any and all suggestions!
Thank you for your time.
It's generally not suitable to put more than one item in a column in a relational database. So create a table like this:
course student
Math John Smith
Math Mary White
Biology Mary White
Chemistry John Smith
English John Smith
Then, you can use this table in JOIN expressions to retrieve the info you want. You may want to have additional tables that describe the essential information about courses and students. Something like:
course instructor time
Math Albert Einstein 10:30
and
student id_number phone
John Smith 1234-5678 555-1212
...or whatever information you need to keep about courses and students.