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
Related
I want to duplicate a specific teacher so that they appear twice in the output of a SELECT statement.
Teacher
Grade
District
Mr. Smith
3rd
West
Mrs. John
4th
South
Mr. Cameron
2nd
North
Mr. Cameron
2nd
North
Kirk Horn
1st
West
Desired result, after duplicating 'Mr. Cameron':
Teacher
Grade
District
Mr. Smith
3rd
West
Mrs. John
4th
South
Mr. Cameron
2nd
North
Mr. Cameron
2nd
North
Mr. Cameron
2nd
North
Mr. Cameron
2nd
North
Kirk Horn
1st
West
What would a SELECT statement look like - without creating a new a table?
I want to do something like this but without the INSERT:
https://dba.stackexchange.com/questions/142414/easiest-way-to-duplicate-rows
If the table is big, and the filter is not selective and backed by an index, then this "trick" avoids a second sequential scan over the table - using PostgreSQL:
SELECT t.* -- or your list of columns
FROM test t
, generate_series(1, CASE WHEN t.teacher = 'Mr. Cameron' THEN 2 ELSE 1 END);
fiddle
It's short syntax for a LATERAL join. See:
What is the difference between a LATERAL JOIN and a subquery in PostgreSQL?
fiddle
If you need more copies, just replace '2' above.
Postgres has a hard time estimating the number of rows to expect with this construct, which may confuse query planning. Stu's variant (doing the same) is slightly more expensive, but easier to estimate for query planning. Syntax needs to be adapted for Postgres:
SELECT teacher, grade, district
FROM test t
JOIN LATERAL (VALUES (1),(2)) x(v) ON v = 1 OR teacher = 'Mr. Cameron';
fiddle
You could use UNION ALL
select Teacher,Grade,District
from test
union all
select Teacher,Grade,District
from test
where Teacher='Mr. Cameron'
order by Teacher;
https://dbfiddle.uk/rBpSL8NW
If you want to force/predict how many duplicate values for Mr. Cameron you will add ,try below query which add only one duplicate value limit 1
(select Teacher,Grade,District
from test
)
union all
(
select Teacher,Grade,District
from test
where Teacher='Mr. Cameron'
limit 1
);
https://dbfiddle.uk/uUX5QD3F
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 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;
I have the following data in my database table, since I'm fairly new to MYSQL i'm having problems in querying it to give me the following output
City Subject
london english
toronto math
london math
london math
toronto english
toronto english
There can only be two subjects, english or math. Im trying to output the data this way, first the query should pick all the distinct items in the city column. Then tell me the count of each subject in that city.
output
city English Math
london 1 2
toronto 2 1
I tried grouping, but since I don't know mysql that well, I realized it just groups the subjects together and eats the cities while grouping.
try this:
SELECT city,
SUM(IF(subject='english',1,0)) AS English,
SUM(IF(subject='math',1,0)) AS Math
FROM foo
GROUP BY city;
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