![]() |
5 Useful MySQL Functions And Control Flows
1: IF()
If statements have been a life saver on many occasions. They are invaluable for checking a value then returning another value different from the one stored in the table. sql Code:
Result: If myColumn1 equals 1 then Yes else No. 2: ELT() Similar to an if else statement. The ELT takes the first value and returns the relative offset from the other arguments. sql Code:
Result: If myColumn1 equals 3 then Three. 3: SOUNDEX() Not as impressive as you would think from your initial perceptions. However, SOUNDEX is good in some instances. The first letter must match though else SOUNDEX will fail. sql Code:
Result: If myColumn1 equals Wildhoney then it would be returned because it sounds like Wyldhoney. 4: DATE_FORMAT() Much confusion has arisen amongst beginners and intermediates to MySQL about how to format the date properly. The DATE_FORMAT function proves precious to output the date in a format you desire. No matter what format the date is stored in, MySQL date or as a UNIX timestamp, DATE_FORMAT will format the date with no questions asked. sql Code:
Note: The other way to format the date is at the programming level, such as with strtotime. Result: For example, 4th Aug, 2007. 5: REGEXP Last but not least, REGEXP can be used in MySQL in place of the more primitive LIKE. It uses the standard PCRE syntax. sql Code:
Result: This will return any strings that begin with W and end in Y. Wildhoney, for instance, will be brought back, but TalkPHP would not. MySQL's REGEXP is case-insensitive and you may also use NOT REGEXP to reverse the outcome. In the above example using NOT REGEXP would return everyone but members that do not begin with W and end in Y. |
Some good functions there, thanks. I didn't know we could do REGEX with MySQL, do you know how it is on performance?
|
Oh, there's a if function for mysql.
Thanks Adam. |
I have absolutely no idea about the speed of MySQL's REGEXP function. If anybody has any idea then I would very much appreciate the information.
|
I'm not sure either, However I may test it out in the near future (tomorrow night), Thankyou for the article though.
|
It is fast, but you can expect similar performance to PHP's preg functions. Thus, basically it would work on a small data set but not on a really large one, from what i noticed, it had a similar performance to "LIKE" which most of you will know isnt very good at all...
|
I've always found COALESCE to be very useful.
6: COALESCE Returns the first non null entry Code:
SELECT COALESCE (column1, column2, '') from tableExtremely useful when you need to check for a null statement or need to set a flag if a value is null. |
I found everything written in this thread useful :D
Anyhow, you should also write something about this: Code:
DATE_SUB(NOW(), INTERVAL 15 MINUTE)php Code:
|
I never knew about
COALESCE. The amount of times I've used conditional statements to check if columns are NULL is unbelievable. Is that a recent addition? As it doesn't even ring a bell. |
One thing i should of mentioned before:
Soundex does not handle non-ascii characters. IE, it will not work for Chinese, Japanese Korean etc :( |
So it's not only me that thinks the Asian languages all sound like random noise? It's comforting to know.
|
Quote:
|
Quote:
Quote:
Word boundary metacharacters, for example, are not \b but [[:<:]] and [[:>:]].Also I believe POSIX keeps on searching for the longest match, whereas PCRE returns the first found match. Sounds a bit vague probably, anyway, my point is that MySQL's REGEXP and PCRE are not the same. |
Interesting. I didn't think other flavours of regex were really utilised these days as PCRE is rather prevalent - and quite possibly the most preferred flavour.
|
I remember learning about most of those functions about 8~ months ago in school, but totally forgot about them.
Thank you. |
thanks they are very useful i will try them
|
Using #1 on a project right now.
|
| All times are GMT. The time now is 12:55 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0