Forum Moderators: coopster
Thankful for any help.
+------------------+--------------------+
¦ originalTitle ¦ sortingTitle ¦
+------------------+--------------------+
¦ The First Book ¦ First Book (The) ¦
¦ A Second Book ¦ Second Book (A) ¦
¦ Third Book ¦ Third Book ¦
¦ An Optional Book ¦ Optional Book (An) ¦
+------------------+--------------------+
SELECT
title
FROM mytable
ORDER BY TRIM(LEADING 'the ' FROM LOWER(title))
;
select IF(title REGEXP 'A +¦An +¦The +',TRIM(LEADING SUBSTRING_INDEX(title,' ',1) from title),title) as mytitle,title from test order by mytitle;
Here's what that gives you. The actual titles are on the right here, the one with the A,An,The stripped on the left. You would display "title", as "mytitle" is just used to order things.
+-------------------------+----------------------------+
¦ mytitle ¦ title ¦
+-------------------------+----------------------------+
¦ Big Cat ¦ The Big Cat ¦
¦ Earthly Story ¦ An Earthly Story ¦
¦ Greatest Show on Earth ¦ The Greatest Show on Earth ¦
¦ sly dog ¦ A sly dog ¦
¦ That Darn Cat ¦ That Darn Cat ¦
+-------------------------+----------------------------+
In this case, the original data looks like this:
+----------------------------+
¦ title ¦
+----------------------------+
¦ A sly dog ¦
¦ The Big Cat ¦
¦ An Earthly Story ¦
¦ The Greatest Show on Earth ¦
¦ That Darn Cat ¦
+----------------------------+
You can google for mysql regexp to find how to tweak this to look for a/an/the case-insensitively. (Thanks, killroy, btw.)
JK
The only issue you are going to have with that is you will need to trim that TRIM once more to get rid of the leading space. Add one more entry to your table such as 'Darn Cat' and you will see what I mean. The blank spaces are being left on the front end of that title.
select TRIM(LEADING ' ' from (IF(title REGEXP 'A +¦An +¦The +',TRIM(LEADING SUBSTRING_INDEX(title,' ',1) from title),title))) as mytitle,title from MyTable order by mytitle;
You end up with
+------------------------+----------------------------+
¦ mytitle ¦ title ¦
+------------------------+----------------------------+
¦ Big Cat ¦ The Big Cat ¦
¦ Big Cat ¦ Big Cat ¦
¦ Earthly Story ¦ An Earthly Story ¦
¦ Greatest Show on Earth ¦ The Greatest Show on Earth ¦
¦ sly dog ¦ A sly dog ¦
¦ That Darn Cat ¦ That Darn Cat ¦
+------------------------+----------------------------+
The only problem with that is that whichever title was entered first will show up first. (Notice how "The Big Cat" comes before "Big Cat.") There may be a way to use concat to tack the result of the regexp back onto the end, but I haven't really explored that.
Good catch, thanks!
(And GRR! the "pre" tags don't preserve my spacing in that table.)
JK
Sorry it took me so long to respond. But I have now tested this:
select TRIM(LEADING ' ' from (IF(title REGEXP 'A +¦An +¦The +',TRIM(LEADING SUBSTRING_INDEX(title,' ',1) from title),title))) as mytitle,title from MyTable order by mytitle;
and it works perfectly. All my titles are now in alphabetic order according to the first real word.
I don't know what I would do without this forum.
I can't figure out why when I use this query the word Kafka is placed after N and also the title "Eat Pray Love" ends up in the wrong spot. Everything else is put in alphabetic order as I want it. I have been fiddling around with the query but can't seem to get it right. Any ideas is greatly appreciated?
select TRIM(LEADING ' ' from (IF(title REGEXP 'A +¦An +¦The +',TRIM(LEADING SUBSTRING_INDEX(title,' ',1) from title),title))) as mytitle,title from MyTable order by mytitle;
The Curious Incident of the Dog in the Night-Time
The Kentucky Derby
My life in the middle ages : a survivor's tale
Mountain Beyond Mountain
Never Let Me Go
Kafka on Shore
Kafka On the Shore
Eat Pray Love: one woman's search for everything across Italy
Seven Sisters
Slow Man
Sun and Shadow
A Sunday at the Pool in Kigali
Sunflower
So, we need to tell it that we only care if the article is at the beginning of the title.
Change this:
REGEXP 'A +¦An +¦The +',
to this:
REGEXP '^A +¦^An +¦^The +',
(Basically, add ^ before A, An, and The. That tells it to only look at the beginning.)
Sorry, common mistake. :-)
JK