Miscellaneous operators and functions
Miscellaneous operators and functions are shown in Table 3-6.
Table 3-6. Miscellaneous operators and functions
| Statement | Output |
|---|---|
| Control flow functions | |
SELECT if(1<0,"yes","no") |
no |
| Encryption functions | |
SELECT password('secret')
|
428567f408994404 |
SELECT encode('secret','shhh')
|
"|ï ¨~ |
SELECT decode('"|ï ¨~','shhh')
|
secret |
| Other functions | |
SELECT database( ) |
winestore |
SELECT user( ) |
dimitria@localhost |
The conditional function if outputs the first string if the expression is true and the second if it is false. This can be used in complex ways. For example, it could be used in an UPDATE statement for intelligent changes to an attribute:
UPDATE customer SET country = if(trim(country)='','Australia',country);
In this case, the SQL statement replaces blank country attributes with Australia and leaves already filled country attributes unaltered.
Authentication and securing data using password( ), encode( ), and decode( ) are discussed in Chapter 9. The functions database( ) and user( ) provide the names of the current database and user.