TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (2) Thread Tools Search this Thread Display Modes
Old 09-06-2007, 03:20 PM   2 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Smile 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:
SELECT
    IF(myColumn1 = 1, 'Yes', 'No') AS myIfColumn
FROM
    myTable

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:
SELECT
    ELT(myColumn1, 'One', 'Two', 'Three', 'Four') AS myELTColumn
FROM
    myTable

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:
SELECT
    myColumn1,
    myColumn2
FROM
    myTable
HAVING
    SOUNDEX(myColumn1) = SOUNDEX('Wyldhoney') AS mySoundexColumn

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:
SELECT
    DATE_FORMAT(myColumn1, '%D %b, %Y') AS myDate
FROM
    myTable

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:
SELECT
    myColumn1,
    myColumn2
FROM
    myTable
WHERE
    myColumn1
REGEXP
    '^W[A-Z]+y$'

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 12-09-2007 at 04:36 PM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 6 Users Say Thank You to Wildhoney For This Useful Post:
Aaron (02-19-2008), bdm (12-12-2007), codefreek (07-14-2009), sjaq (01-02-2008), Village Idiot (01-01-2008), webtuto (01-01-2008)
Old 09-06-2007, 03:52 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Some good functions there, thanks. I didn't know we could do REGEX with MySQL, do you know how it is on performance?
Karl is offline  
Reply With Quote
Old 09-06-2007, 04:11 PM   #3 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default

Oh, there's a if function for mysql.

Thanks Adam.
Haris is offline  
Reply With Quote
Old 09-06-2007, 05:33 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-06-2007, 08:10 PM   #5 (permalink)
The Contributor
 
localhost's Avatar
 
Join Date: Apr 2007
Location: Hampshire
Posts: 28
Thanks: 1
localhost is on a distinguished road
Default

I'm not sure either, However I may test it out in the near future (tomorrow night), Thankyou for the article though.
__________________
Send a message via MSN to localhost Send a message via Skype™ to localhost
localhost is offline  
Reply With Quote
Old 09-06-2007, 10:22 PM   #6 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

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...
bluesaga is offline  
Reply With Quote
Old 11-30-2007, 10:54 PM   #7 (permalink)
The Wanderer
 
Sand_Devil's Avatar
 
Join Date: Nov 2007
Location: El Paso, TX
Posts: 7
Thanks: 1
Sand_Devil is on a distinguished road
Default

I've always found COALESCE to be very useful.
6: COALESCE

Returns the first non null entry
Code:
SELECT COALESCE (column1, column2, '') from table
Result: if column1 not null, then returns column1, if column1 is null and column2 is not, then will return column2, if both column1 and column2 is null, will return an empty string.

Extremely useful when you need to check for a null statement or need to set a flag if a value is null.
Send a message via ICQ to Sand_Devil Send a message via AIM to Sand_Devil Send a message via MSN to Sand_Devil Send a message via Yahoo to Sand_Devil
Sand_Devil is offline  
Reply With Quote
The Following User Says Thank You to Sand_Devil For This Useful Post:
SOCK (12-09-2007)
Old 11-30-2007, 11:04 PM   #8 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

I found everything written in this thread useful :D

Anyhow, you should also write something about this:
Code:
DATE_SUB(NOW(), INTERVAL 15 MINUTE)
Because my experience with it is very nice. For example, when you're using a usersystem, and want to have automatic logouts for everyone who hasn't done anything for 15 minutes.

php Code:
UPDATE
        `$this->db->table['users']`
SET
        `$this->db->col['user_session']` = ''
WHERE
        DATE_SUB(NOW(), INTERVAL 15 MINUTE) > `$this->db->col['user_last_action']`;
Tanax is offline  
Reply With Quote
Old 11-30-2007, 11:04 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-01-2007, 12:18 AM   #10 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

One thing i should of mentioned before:

Soundex does not handle non-ascii characters. IE, it will not work for Chinese, Japanese Korean etc :(
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 12-01-2007, 02:53 AM   #11 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

So it's not only me that thinks the Asian languages all sound like random noise? It's comforting to know.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-01-2007, 11:16 PM   #12 (permalink)
The Wanderer
 
Sand_Devil's Avatar
 
Join Date: Nov 2007
Location: El Paso, TX
Posts: 7
Thanks: 1
Sand_Devil is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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.
I have only been coding in mysql and php for the last year and a half. It was one of the first functions that I discovered (out of necessity).
Send a message via ICQ to Sand_Devil Send a message via AIM to Sand_Devil Send a message via MSN to Sand_Devil Send a message via Yahoo to Sand_Devil
Sand_Devil is offline  
Reply With Quote
Old 12-09-2007, 04:42 PM   #13 (permalink)
The Contributor
RegEx Guru 
 
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
Geert is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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.
Quote from http://dev.mysql.com/doc/refman/5.0/en/regexp.html:
Quote:
MySQL uses Henry Spencer's implementation of regular expressions, which is aimed at conformance with POSIX 1003.2.
Just for the record, that's another regex flavor than PCRE. I believe it's the same as the PHP ereg functions.

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.
__________________
Kohana - PHP5 framework
Geert is offline  
Reply With Quote
Old 12-09-2007, 05:20 PM   #14 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-12-2007, 01:32 PM   #15 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

I remember learning about most of those functions about 8~ months ago in school, but totally forgot about them.

Thank you.
bdm is offline  
Reply With Quote
Old 01-01-2008, 03:01 PM   #16 (permalink)
The Addict
 
webtuto's Avatar
 
Join Date: Dec 2007
Location: morocco
Posts: 221
Thanks: 19
webtuto is on a distinguished road
Default

thanks they are very useful i will try them
Send a message via MSN to webtuto Send a message via Yahoo to webtuto Send a message via Skype™ to webtuto
webtuto is offline  
Reply With Quote
Old 01-02-2008, 08:44 PM   #17 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Using #1 on a project right now.
__________________

Village Idiot is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/mysql-databases/1020-5-useful-mysql-functions-control-flows.html
Posted By For Type Date
MySQL 5 Useful MySQL Functions And Control Flows Tutorial This thread Refback 12-23-2007 05:14 AM
MySQL Miscellaneous 5 Useful MySQL Functions And Control Flows Tutorial This thread Refback 12-22-2007 08:38 AM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:35 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design