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
Related
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.
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 a table where one field contains a free to choose text. Some of these texts are identical, some are not. So as an example, this may look like this:
Joe
Jim
Jack
Jack
Jim
Jack
Jane
Now I want to list all the contents of these fields, but compared on a content level so that every data is shown only once. Means the result from my data have to look like this:
Joe
Jim
Jack
Jane
The double-entries Jim and Jack are shown only once although they are contained more often.
My question: is there a SQL-statement which covers this or do I have to filter these data in the result?
Thanks!
Select distinct fieldname from your_table_name
SELECT DISTINCT nameField
FROM YourTable
I have a simple question that I wasn't really sure how to search for (or title!). I apologize if this has been asked a million times. For the following table, how do I generate a report that will detail the number of companies that a person has worked for and how many people have also worked for that same number? So, for example, this table should return:
people, companiesperperson
1, 1
2, 2
1, 3
for the following table called personalinfo:
id_number first last company
1 John Doe Intel
2 John Doe Microsoft
3 Phil Jenkins Amgen
4 Phil Jenkins Bayer
5 Phil Jenkins Sanofi
6 Josh Edwards Walgreens
7 Amy Dill URS
8 Amy Dill ARCADIS
Let me know if this is still confusing and if I can further clarify what I am looking to do.
Thanks!
This is a rough estimate of the query but
SELECT count as companiesperperson, COUNT(first, last) as people FROM
(SELECT COUNT(company) as count, first, last FROM personalinfo GROUP BY (first, last)) as a
GROUP BY count
To explain the query first in the subquery we are asking for the names and count of companies after splitting up all the rows by names
Then in the outer query we split up all the rows by their count and ask how many unique names can be found in each group.
There may be a few syntax errors I've left straggling but the group by feature is really what's essential to understanding how to solve this question.
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;