rot13 function on MYSQL - mysql

I've searched all the internet looking for this.
Anyone has a rot13 function on MYSQL?
Edit: It has to be a function in SQL.

Made at brute force:
CREATE FUNCTION rot13(stringIn VARCHAR(500)) RETURNS VARCHAR(500)
BEGIN
DECLARE v1 INT DEFAULT 1;
DECLARE stringOut VARCHAR(200) DEFAULT '';
DECLARE str VARCHAR(1) DEFAULT '';
WHILE v1 <= LENGTH(stringIn) DO
SET str = SUBSTR(stringIn,v1,1);
CASE BINARY str
WHEN 'A' THEN SET stringOut = CONCAT(stringOut , 'N');
WHEN 'a' THEN SET stringOut = CONCAT(stringOut , 'n');
WHEN 'B' THEN SET stringOut = CONCAT(stringOut , 'O');
WHEN 'b' THEN SET stringOut = CONCAT(stringOut , 'o');
WHEN 'C' THEN SET stringOut = CONCAT(stringOut , 'P');
WHEN 'c' THEN SET stringOut = CONCAT(stringOut , 'p');
WHEN 'D' THEN SET stringOut = CONCAT(stringOut , 'Q');
WHEN 'd' THEN SET stringOut = CONCAT(stringOut , 'q');
WHEN 'E' THEN SET stringOut = CONCAT(stringOut , 'R');
WHEN 'e' THEN SET stringOut = CONCAT(stringOut , 'r');
WHEN 'F' THEN SET stringOut = CONCAT(stringOut , 'S');
WHEN 'f' THEN SET stringOut = CONCAT(stringOut , 's');
WHEN 'G' THEN SET stringOut = CONCAT(stringOut , 'T');
WHEN 'g' THEN SET stringOut = CONCAT(stringOut , 't');
WHEN 'H' THEN SET stringOut = CONCAT(stringOut , 'U');
WHEN 'h' THEN SET stringOut = CONCAT(stringOut , 'u');
WHEN 'I' THEN SET stringOut = CONCAT(stringOut , 'V');
WHEN 'i' THEN SET stringOut = CONCAT(stringOut , 'v');
WHEN 'J' THEN SET stringOut = CONCAT(stringOut , 'W');
WHEN 'j' THEN SET stringOut = CONCAT(stringOut , 'w');
WHEN 'K' THEN SET stringOut = CONCAT(stringOut , 'X');
WHEN 'k' THEN SET stringOut = CONCAT(stringOut , 'x');
WHEN 'L' THEN SET stringOut = CONCAT(stringOut , 'Y');
WHEN 'l' THEN SET stringOut = CONCAT(stringOut , 'y');
WHEN 'M' THEN SET stringOut = CONCAT(stringOut , 'Z');
WHEN 'm' THEN SET stringOut = CONCAT(stringOut , 'z');
WHEN 'N' THEN SET stringOut = CONCAT(stringOut , 'A');
WHEN 'n' THEN SET stringOut = CONCAT(stringOut , 'a');
WHEN 'O' THEN SET stringOut = CONCAT(stringOut , 'B');
WHEN 'o' THEN SET stringOut = CONCAT(stringOut , 'b');
WHEN 'P' THEN SET stringOut = CONCAT(stringOut , 'C');
WHEN 'p' THEN SET stringOut = CONCAT(stringOut , 'c');
WHEN 'Q' THEN SET stringOut = CONCAT(stringOut , 'D');
WHEN 'q' THEN SET stringOut = CONCAT(stringOut , 'd');
WHEN 'R' THEN SET stringOut = CONCAT(stringOut , 'E');
WHEN 'r' THEN SET stringOut = CONCAT(stringOut , 'e');
WHEN 'S' THEN SET stringOut = CONCAT(stringOut , 'F');
WHEN 's' THEN SET stringOut = CONCAT(stringOut , 'f');
WHEN 'T' THEN SET stringOut = CONCAT(stringOut , 'G');
WHEN 't' THEN SET stringOut = CONCAT(stringOut , 'g');
WHEN 'U' THEN SET stringOut = CONCAT(stringOut , 'H');
WHEN 'u' THEN SET stringOut = CONCAT(stringOut , 'h');
WHEN 'V' THEN SET stringOut = CONCAT(stringOut , 'I');
WHEN 'v' THEN SET stringOut = CONCAT(stringOut , 'i');
WHEN 'W' THEN SET stringOut = CONCAT(stringOut , 'J');
WHEN 'w' THEN SET stringOut = CONCAT(stringOut , 'j');
WHEN 'X' THEN SET stringOut = CONCAT(stringOut , 'K');
WHEN 'x' THEN SET stringOut = CONCAT(stringOut , 'k');
WHEN 'Y' THEN SET stringOut = CONCAT(stringOut , 'L');
WHEN 'y' THEN SET stringOut = CONCAT(stringOut , 'l');
WHEN 'Z' THEN SET stringOut = CONCAT(stringOut , 'M');
WHEN 'z' THEN SET stringOut = CONCAT(stringOut , 'm');
END CASE;
SET v1 = v1 + 1;
END WHILE;
RETURN stringOut;
END;

How about something like...
SELECT SUBSTR('nopqrstuvwxyzabcdefghijklm',ASCII('b')-96,1);
I should point out that I've undertaken no research on this - so I might have misconstrued what rot13 is.

Related

SQL HTML Email Column Format instead of row format

I'm trying to figure out how to flip this from a table format to a column format.
Hi, I know how to create a SQL HTML Email via table format: i.e
Customer Address City
Joe 123 Here Oakland
but i need to be able to flip this to look like
Customer: Joe
Address: 123 Here
City: Oakland
I'm using SQL 2012
Any help would be appreciated
Please advise
DECLARE #xmlinv NVARCHAR(MAX);
DECLARE #bodyinv NVARCHAR(MAX);
DECLARE #trrowinv NVARCHAR(MAX);
DECLARE #tempemaillistinv NVARCHAR(MAX);
DECLARE #subject_anomaly NVARCHAR(MAX);
SET #subject_anomaly
= N'Inventory Adjustments ' + CAST(#COUNTSinv AS VARCHAR(3)) + N' '
+ LEFT(CONVERT(VARCHAR(10), GETDATE(), 101), 3) + SUBSTRING(CONVERT(VARCHAR(10), GETDATE(), 101), 4, 3)
+ RIGHT(CONVERT(VARCHAR(10), GETDATE(), 101), 2);
SET #xmlinv
= CAST(
(
SELECT T4.product AS 'td',
'',
T4.available AS 'td',
'',
CASE
WHEN T4.quarantine IS NULL THEN
0
ELSE
T4.quarantine
END AS 'td',
'',
CASE
WHEN T4.labels IS NULL THEN
0
ELSE
T4.labels
END AS 'td',
'',
T4.MinLog AS 'td',
'',
T.available AS 'td',
'',
CASE
WHEN T.quarantine IS NULL THEN
0
ELSE
T.quarantine
END AS 'td',
'',
CASE
WHEN T.labels IS NULL THEN
0
ELSE
T.labels
END AS 'td',
'',
T.MinLog AS 'td'
--SELECT T4.product,T4.available,T4.quarantine,T4.labels,T4.MinLog,T.available,T.quarantine,T.labels,T.MinLog
FROM #t4 AS T4
LEFT OUTER JOIN #t3 AS T3
ON T4.MaxRow2 = T3.MaxRow
AND T4.product = T3.product
LEFT OUTER JOIN #t2 AS T2
ON T4.MaxRow2 = T2.MaxRow
AND T4.product = T2.product
LEFT OUTER JOIN #temp AS T
ON T4.MaxRow2 = T.therow + 1
AND T4.product = T.product
ORDER BY 1
--ORDER BY 1,3 desc
FOR XML PATH('tr'), ELEMENTS
)
AS
NVARCHAR(MAX));
SET #bodyinv
= N'<html><H2>Adjustments</H2><body bgcolor=white><table border=1 style=''border:1px;background-color:#FFFFCC;bordercolor:#FFCC00'' cellpadding=''1'' cellspacing=''0''>';
SET #trrowinv
= N'<tr><th>Product</th><th>NewAvailable</th><th>NewQuarantine</th><th>NewLabels</th><th>LastUpdate</th><th>OldAvailable</th><th>OldQuarantine</th><th>OldLabels</th><th>PriorUpdate</th></tr>';
SET #bodyinv = #bodyinv + #trrowinv + #xmlinv + N'</table></body></html>';
SET #tempemaillistinv = N'abc#abc.com';
EXEC [msdb].[dbo].[sp_send_dbmail] #recipients = #tempemaillistinv,
#body = #bodyinv,
#body_format = 'HTML',
#subject = #subject_anomaly,
#profile_name = 'abc';

SQL ORDER BY multiple fields with sub orders

I'm trying to make a query to get the list of a students class ordered by the type defined, sex and name.
I have the next query:
SELECT * FROM STUDENTS
ORDER BY FIELD(type, 'b', 'p', 'j', 'i', 'a', 'c', 'v'), FIELD(sex, 'M', 'F'), `name` ASC
I'm getting that order, first type, and then both sex from that type and name, but I need to define some types and sex together, something like this:
type = 'b', sex = 'M', name
type = 'b', sex = 'F', name
type = 'p', sex = 'M', name
type = 'p', sex = 'F', name
type = 'i', sex = 'M', name
type = 'a', sex = 'M', name
type = 'c', sex = 'M', name
type = 'v', sex = 'M', name
type = 'j', sex = 'M', name
type = 'i', sex = 'F', name
type = 'a', sex = 'F', name
type = 'c', sex = 'F', name
type = 'v', sex = 'F', name
type = 'j', sex = 'F', name
You can always use a CASE statement to create the expression you want
SQL DEMO
SELECT *
FROM Table1
ORDER BY CASE WHEN type = 'b' and sex = 'M' THEN 1
WHEN type = 'b' and sex = 'F' THEN 2
WHEN type = 'p' and sex = 'M' THEN 3
WHEN type = 'p' and sex = 'F' THEN 4
....
ELSE 9999
END,
name

Hive Query issue - Invalid table alias or column reference

I have a query which contains Case statement and the output of the CASE is used to compare another column in Hive.I am not able to run the same query.Here is the query.
SELECT
AL1.RECORD_ID,
AL1.CARRIER_CODE,
AL1.ORIG_AP_CTY_CDE,
AL1.ORIG_STATE_CODE,
AL1.ORIG_COUNTRY_CODE,
AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER,
CASE
WHEN AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER > 99 THEN CONCAT(CAST(AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER/10 AS INT), '0')
WHEN AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER IN ('1','2','4','6','7','8','9') THEN '000'
ELSE AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER
END AS ALL_ORIG_GEOGRAPHIC_ZONE,
AL1.ORIG_WORLD_AREA_NUMBER,
AL1.DEST_AP_CTY_CDE,
AL1.DEST_STATE_CODE,
AL1.DEST_COUNTRY_CODE,
AL1.DEST_GEOGRAPHIC_ZONE_NUMBER,
CASE
WHEN AL1.DEST_GEOGRAPHIC_ZONE_NUMBER > 99 THEN CONCAT(CAST(AL1.DEST_GEOGRAPHIC_ZONE_NUMBER/10 AS INT), '0')
WHEN AL1.DEST_GEOGRAPHIC_ZONE_NUMBER IN ('1','2','4','6','7','8','9') THEN '000'
ELSE AL1.DEST_GEOGRAPHIC_ZONE_NUMBER
END AS ALL_DEST_GEOGRAPHIC_ZONE,
AL1.DEST_WORLD_AREA_NUMBER,
AL1.FRBS_CDE,
AL1.OW_RT_CDE,
AL1.PUB_RULE_TRF_NUM,
AL1.FARE_RULE_NUM,
AL1.PUB_RTG_NUM,
AL1.PUB_FTNTE_ID_CDE,
AL1.FARE_TYPE_CODE,
AL1.SEASON_TYPE_CODE,
AL1.DAY_OF_WEEK_TYPE_CODE,
AL2.CATEGORY_CONTROL_ID,
AL2.CATEGORY_NUMBER AS GROUP_CATEGORY_NUMBER,
AL2.SEQUENCE_NUMBER,
AL2.LOCATION1_TYPE_CODE,
AL2.LOCATION1_CODE,
AL2.LOCATION2_TYPE_CODE,
AL2.LOCATION2_CODE,
AL2.FARE_CLASS_CODE,
AL2.GENERAL_RULE_TARIFF_NUMBER,
AL2.GENERAL_RULE_NUMBER,
AL2.GENERAL_RULE_IND,
'F' AS REC_IND
FROM TMP_TD_CNSTR_WINNING_FARES AL1
INNER JOIN TMP_TD_CNSTRPOST_CAT_CONTROL_ONLY_LIMITED_F AL2
ON (
AL1.CARRIER_CODE=AL2.CARRIER_CODE
AND AL1.PUB_RULE_TRF_NUM=AL2.TARIFF_NUMBER
AND AL1.FARE_RULE_NUM = AL2.RULE_FOOTNOTE_CODE
AND AL1.PUB_RTG_NUM = AL2.ROUTING_NUMBER
AND AL1.SEASON_TYPE_CODE = AL2.SEASON_TYPE_CODE
AND AL1.PUB_FTNTE_ID_CDE = AL2.FOOTNOTE_CODE
AND AL1.OW_RT_CDE = AL2.OW_RT_IND
AND AL1.FARE_TYPE_CODE = AL2.FARE_TYPE_CODE
AND AL1.DAY_OF_WEEK_TYPE_CODE = AL2. DAY_OF_WEEK_TYPE_CODE
)
INNER JOIN TMP_TD_CNSTRPOST_FRBS_MATCH_F AL4
ON ( AL1.CARRIER_CODE = AL4.CARRIER_CODE
AND AL1.PUB_RULE_TRF_NUM = AL4.PUB_RULE_TRF_NUM
AND AL1.FARE_RULE_NUM = AL4.RULE_NUM
AND AL1.FRBS_CDE = AL4.FRBS_CDE
AND AL2.FARE_CLASS_CODE = AL4.FARE_CLASS_CODE
)
WHERE
( (
(COALESCE(LOCATION1_TYPE_CODE, '') = '') OR
(LOCATION1_TYPE_CODE = 'C' AND LOCATION1_CODE = ORIG_AP_CTY_CDE) OR
(LOCATION1_TYPE_CODE = 'S' AND LOCATION1_CODE = ORIG_STATE_CODE) OR
(LOCATION1_TYPE_CODE = 'N' AND LOCATION1_CODE = ORIG_COUNTRY_CODE) OR
(LOCATION1_TYPE_CODE = 'Z' AND LOCATION1_CODE = ORIG_GEOGRAPHIC_ZONE_NUMBER) OR
(LOCATION1_TYPE_CODE = 'Z' AND LOCATION1_CODE = ALL_ORIG_GEOGRAPHIC_ZONE) OR
(LOCATION1_TYPE_CODE = 'A' AND LOCATION1_CODE = ORIG_WORLD_AREA_NUMBER)
)
AND
(
(COALESCE(LOCATION2_TYPE_CODE, '') = '') OR
(LOCATION2_TYPE_CODE = 'C' AND LOCATION2_CODE = DEST_AP_CTY_CDE) OR
(LOCATION2_TYPE_CODE = 'S' AND LOCATION2_CODE = DEST_STATE_CODE) OR
(LOCATION2_TYPE_CODE = 'N' AND LOCATION2_CODE = DEST_COUNTRY_CODE) OR
(LOCATION2_TYPE_CODE = 'Z' AND LOCATION2_CODE = DEST_GEOGRAPHIC_ZONE_NUMBER) OR
(LOCATION2_TYPE_CODE = 'Z' AND LOCATION2_CODE = ALL_DEST_GEOGRAPHIC_ZONE) OR
(LOCATION2_TYPE_CODE = 'A' AND LOCATION2_CODE = DEST_WORLD_AREA_NUMBER)
)
)
OR
((
(COALESCE(LOCATION1_TYPE_CODE, '') = '') OR
(LOCATION1_TYPE_CODE = 'C' AND LOCATION1_CODE = DEST_AP_CTY_CDE) OR
(LOCATION1_TYPE_CODE = 'S' AND LOCATION1_CODE = DEST_STATE_CODE) OR
(LOCATION1_TYPE_CODE = 'N' AND LOCATION1_CODE = DEST_COUNTRY_CODE) OR
(LOCATION1_TYPE_CODE = 'Z' AND LOCATION1_CODE = DEST_GEOGRAPHIC_ZONE_NUMBER) OR
(LOCATION1_TYPE_CODE = 'Z' AND LOCATION1_CODE = ALL_DEST_GEOGRAPHIC_ZONE) OR
(LOCATION1_TYPE_CODE = 'A' AND LOCATION1_CODE = DEST_WORLD_AREA_NUMBER)
)
AND
(
(COALESCE(LOCATION2_TYPE_CODE, '') = '') OR
(LOCATION2_TYPE_CODE = 'C' AND LOCATION2_CODE = ORIG_AP_CTY_CDE) OR
(LOCATION2_TYPE_CODE = 'S' AND LOCATION2_CODE = ORIG_STATE_CODE) OR
(LOCATION2_TYPE_CODE = 'N' AND LOCATION2_CODE = ORIG_COUNTRY_CODE) OR
(LOCATION2_TYPE_CODE = 'Z' AND LOCATION2_CODE = ORIG_GEOGRAPHIC_ZONE_NUMBER) OR
(LOCATION2_TYPE_CODE = 'Z' AND LOCATION2_CODE = ALL_ORIG_GEOGRAPHIC_ZONE) OR
(LOCATION2_TYPE_CODE = 'A' AND LOCATION2_CODE = ORIG_WORLD_AREA_NUMBER)
)
)
;
The error I am encountering is
FAILED: SemanticException [Error 10004]: Line 72:59 Invalid table alias or column reference 'ALL_ORIG_GEOGRAPHIC_ZONE
Please help me resolving the issue.Thanks in Advance!!
The where clause is evaluated before the select clause, so you can't use ALL_DEST_GEOGRAPHIC_ZONE in where clause.
You can try it in a derived table or having clause.
relevant question
Thanks Jerrick.It worked for me using derived table.Here is the updated query.
SELECT
*
FROM (SELECT
AL1.RECORD_ID,
AL1.CARRIER_CODE,
AL1.ORIG_AP_CTY_CDE,
AL1.ORIG_STATE_CODE,
AL1.ORIG_COUNTRY_CODE,
AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER,
CASE
WHEN AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER > 99 THEN CONCAT((CAST(AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER / 10 AS int)), '0')
WHEN AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER IN ('1', '2', '4', '6', '7', '8', '9') THEN '000'
ELSE AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER
END AS ALL_ORIG_GEOGRAPHIC_ZONE,
AL1.ORIG_WORLD_AREA_NUMBER,
AL1.DEST_AP_CTY_CDE,
AL1.DEST_STATE_CODE,
AL1.DEST_COUNTRY_CODE,
AL1.DEST_GEOGRAPHIC_ZONE_NUMBER,
CASE
WHEN AL1.DEST_GEOGRAPHIC_ZONE_NUMBER > 99 THEN CONCAT((CAST(AL1.DEST_GEOGRAPHIC_ZONE_NUMBER / 10 AS int)), '0')
WHEN AL1.DEST_GEOGRAPHIC_ZONE_NUMBER IN ('1', '2', '4', '6', '7', '8', '9') THEN '000'
ELSE AL1.DEST_GEOGRAPHIC_ZONE_NUMBER
END AS ALL_DEST_GEOGRAPHIC_ZONE,
AL1.DEST_WORLD_AREA_NUMBER,
AL1.FRBS_CDE,
AL1.OW_RT_CDE,
AL1.PUB_RULE_TRF_NUM,
AL1.FARE_RULE_NUM,
AL1.PUB_RTG_NUM,
AL1.PUB_FTNTE_ID_CDE,
AL1.FARE_TYPE_CODE,
AL1.SEASON_TYPE_CODE,
AL1.DAY_OF_WEEK_TYPE_CODE,
AL2.CATEGORY_CONTROL_ID,
AL2.CATEGORY_NUMBER AS GROUP_CATEGORY_NUMBER,
AL2.SEQUENCE_NUMBER,
AL2.LOCATION1_TYPE_CODE,
AL2.LOCATION1_CODE,
AL2.LOCATION2_TYPE_CODE,
AL2.LOCATION2_CODE,
AL2.FARE_CLASS_CODE,
AL2.GENERAL_RULE_TARIFF_NUMBER,
AL2.GENERAL_RULE_NUMBER,
AL2.GENERAL_RULE_IND,
'F' AS REC_IND
FROM TMP_TD_CNSTR_WINNING_FARES AL1
INNER JOIN TMP_TD_CNSTRPOST_CAT_CONTROL_ONLY_LIMITED_F AL2
ON (
AL1.CARRIER_CODE = AL2.CARRIER_CODE
AND AL1.PUB_RULE_TRF_NUM = AL2.TARIFF_NUMBER
AND AL1.FARE_RULE_NUM = AL2.RULE_FOOTNOTE_CODE
AND AL1.PUB_RTG_NUM = AL2.ROUTING_NUMBER
AND AL1.SEASON_TYPE_CODE = AL2.SEASON_TYPE_CODE
AND AL1.PUB_FTNTE_ID_CDE = AL2.FOOTNOTE_CODE
AND AL1.OW_RT_CDE = AL2.OW_RT_IND
AND AL1.FARE_TYPE_CODE = AL2.FARE_TYPE_CODE
AND AL1.DAY_OF_WEEK_TYPE_CODE = AL2.DAY_OF_WEEK_TYPE_CODE
)
INNER JOIN TMP_TD_CNSTRPOST_FRBS_MATCH_F AL4
ON (AL1.CARRIER_CODE = AL4.CARRIER_CODE
AND AL1.PUB_RULE_TRF_NUM = AL4.PUB_RULE_TRF_NUM
AND AL1.FARE_RULE_NUM = AL4.RULE_NUM
AND AL1.FRBS_CDE = AL4.FRBS_CDE
AND AL2.FARE_CLASS_CODE = AL4.FARE_CLASS_CODE
)) AS WF
WHERE ((
(COALESCE(LOCATION1_TYPE_CODE, '') = '')
OR (LOCATION1_TYPE_CODE = 'C'
AND LOCATION1_CODE = ORIG_AP_CTY_CDE)
OR (LOCATION1_TYPE_CODE = 'S'
AND LOCATION1_CODE = ORIG_STATE_CODE)
OR (LOCATION1_TYPE_CODE = 'N'
AND LOCATION1_CODE = ORIG_COUNTRY_CODE)
OR (LOCATION1_TYPE_CODE = 'Z'
AND LOCATION1_CODE = ORIG_GEOGRAPHIC_ZONE_NUMBER)
OR (LOCATION1_TYPE_CODE = 'Z'
AND LOCATION1_CODE = WF.ALL_ORIG_GEOGRAPHIC_ZONE)
OR (LOCATION1_TYPE_CODE = 'A'
AND LOCATION1_CODE = ORIG_WORLD_AREA_NUMBER)
)
AND (
(COALESCE(LOCATION2_TYPE_CODE, '') = '')
OR (LOCATION2_TYPE_CODE = 'C'
AND LOCATION2_CODE = DEST_AP_CTY_CDE)
OR (LOCATION2_TYPE_CODE = 'S'
AND LOCATION2_CODE = DEST_STATE_CODE)
OR (LOCATION2_TYPE_CODE = 'N'
AND LOCATION2_CODE = DEST_COUNTRY_CODE)
OR (LOCATION2_TYPE_CODE = 'Z'
AND LOCATION2_CODE = DEST_GEOGRAPHIC_ZONE_NUMBER)
OR (LOCATION2_TYPE_CODE = 'Z'
AND LOCATION2_CODE = WF.ALL_DEST_GEOGRAPHIC_ZONE)
OR (LOCATION2_TYPE_CODE = 'A'
AND LOCATION2_CODE = DEST_WORLD_AREA_NUMBER)
)
)
OR ((
(COALESCE(LOCATION1_TYPE_CODE, '') = '')
OR (LOCATION1_TYPE_CODE = 'C'
AND LOCATION1_CODE = DEST_AP_CTY_CDE)
OR (LOCATION1_TYPE_CODE = 'S'
AND LOCATION1_CODE = DEST_STATE_CODE)
OR (LOCATION1_TYPE_CODE = 'N'
AND LOCATION1_CODE = DEST_COUNTRY_CODE)
OR (LOCATION1_TYPE_CODE = 'Z'
AND LOCATION1_CODE = DEST_GEOGRAPHIC_ZONE_NUMBER)
OR (LOCATION1_TYPE_CODE = 'Z'
AND LOCATION1_CODE = WF.ALL_DEST_GEOGRAPHIC_ZONE)
OR (LOCATION1_TYPE_CODE = 'A'
AND LOCATION1_CODE = DEST_WORLD_AREA_NUMBER)
)
AND (
(COALESCE(LOCATION2_TYPE_CODE, '') = '')
OR (LOCATION2_TYPE_CODE = 'C'
AND LOCATION2_CODE = ORIG_AP_CTY_CDE)
OR (LOCATION2_TYPE_CODE = 'S'
AND LOCATION2_CODE = ORIG_STATE_CODE)
OR (LOCATION2_TYPE_CODE = 'N'
AND LOCATION2_CODE = ORIG_COUNTRY_CODE)
OR (LOCATION2_TYPE_CODE = 'Z'
AND LOCATION2_CODE = ORIG_GEOGRAPHIC_ZONE_NUMBER)
OR (LOCATION2_TYPE_CODE = 'Z'
AND LOCATION2_CODE = WF.ALL_ORIG_GEOGRAPHIC_ZONE)
OR (LOCATION2_TYPE_CODE = 'A'
AND LOCATION2_CODE = ORIG_WORLD_AREA_NUMBER)
)
)
;

Mysql Equivalent of php metaphone and soundex

I am working on an app where user's current playing song title is fetched and we look in the mysql database to see who else is playing a similar song.since the same song might be with many varied titles on everyone's phone , we need a way to effectively find as close results as possible.
The process that we are using right now gets all the songs from the table then do a foreach and compare each entry in the resultset with user's song.
Here is a part of the function we have used:
$all_results = $db->select($sql);//all db entries from the songs table
foreach ( $all_results as $u ) {
$toTest = strtolower( $u['last_song'] );
$toTest = preg_replace('/[^A-Za-z0-9]/', ' ', $toTest);
$score = 0;
$score = $this->calSim( $user_last_song, $toTest, 0 ); //user last song is the current song being played by the user
if ( $score > 1 ) { //if at least there is some match then compare by another method
$score = $this->calMetaphone($user_last_song, $toTest, $score);
}
if ( $score > 5 ) {
//song matches.Push into a final array
}
}
=======================================
here are the 2 custom functions:
public function calMetaphone ( $x, $y, $pts ) {
$x = metaphone( $x );
$y = metaphone( $y );
$pts = $this->calSim( $x, $y, $pts );
return $pts;
}
public function calSim ( $x, $y, $pts ) {
similar_text($x, $y, $sim);
//echo "Similarity is :$sim<br>";
if ( $sim >= 90 ) {
$pts = $pts + 5;
} else if ( $sim >= 80 ) {
$pts = $pts + 4;
}
if ( $sim >= 70 ) {
$pts = $pts + 3;
}
if ( $sim >= 60 ) {
$pts = $pts + 2;
} else {
$pts = $pts + 0;
}
return $pts;
}
But I know this is a real bad way of doing it.If there are large number of entries in database it might take forever to compare them all 1 by 1.
Can anyone tell me the correct method that should be followed here.
Thanks
Karam
I can't take any credit for this but when I needed a similar function I found this (can't remember where though) for a metaphone:-
DROP FUNCTION `func_Double_Metaphone`//
CREATE DEFINER=`aaaa`#`%` FUNCTION `func_Double_Metaphone`(st VARCHAR(55)) RETURNS varchar(128) CHARSET utf8
NO SQL
BEGIN
DECLARE length, first, last, pos, prevpos, is_slavo_germanic SMALLINT;
DECLARE pri, sec VARCHAR(45) DEFAULT '';
DECLARE ch CHAR(1);
SET first = 3;
SET length = CHAR_LENGTH(st);
SET last = first + length -1;
SET st = CONCAT(REPEAT('-', first -1), UCASE(st), REPEAT(' ', 5)); SET is_slavo_germanic = (st LIKE '%W%' OR st LIKE '%K%' OR st LIKE '%CZ%'); SET pos = first; IF SUBSTRING(st, first, 2) IN ('GN', 'KN', 'PN', 'WR', 'PS') THEN
SET pos = pos + 1;
END IF;
IF SUBSTRING(st, first, 1) = 'X' THEN
SET pri = 'S', sec = 'S', pos = pos + 1; END IF;
WHILE pos <= last DO
SET prevpos = pos;
SET ch = SUBSTRING(st, pos, 1); CASE
WHEN ch IN ('A', 'E', 'I', 'O', 'U', 'Y') THEN
IF pos = first THEN SET pri = CONCAT(pri, 'A'), sec = CONCAT(sec, 'A'), pos = pos + 1; ELSE
SET pos = pos + 1;
END IF;
WHEN ch = 'B' THEN
IF SUBSTRING(st, pos+1, 1) = 'B' THEN
SET pri = CONCAT(pri, 'P'), sec = CONCAT(sec, 'P'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'P'), sec = CONCAT(sec, 'P'), pos = pos + 1; END IF;
WHEN ch = 'C' THEN
IF (pos > (first + 1) AND SUBSTRING(st, pos-2, 1) NOT IN ('A', 'E', 'I', 'O', 'U', 'Y') AND SUBSTRING(st, pos-1, 3) = 'ACH' AND
(SUBSTRING(st, pos+2, 1) NOT IN ('I', 'E') OR SUBSTRING(st, pos-2, 6) IN ('BACHER', 'MACHER'))) THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSEIF pos = first AND SUBSTRING(st, first, 6) = 'CAESAR' THEN
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'S'), pos = pos + 2; ELSEIF SUBSTRING(st, pos, 4) = 'CHIA' THEN SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSEIF SUBSTRING(st, pos, 2) = 'CH' THEN
IF pos > first AND SUBSTRING(st, pos, 4) = 'CHAE' THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'X'), pos = pos + 2; ELSEIF pos = first AND (SUBSTRING(st, pos+1, 5) IN ('HARAC', 'HARIS') OR
SUBSTRING(st, pos+1, 3) IN ('HOR', 'HYM', 'HIA', 'HEM')) AND SUBSTRING(st, first, 5) != 'CHORE' THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSEIF SUBSTRING(st, first, 4) IN ('VAN ', 'VON ') OR SUBSTRING(st, first, 3) = 'SCH'
OR SUBSTRING(st, pos-2, 6) IN ('ORCHES', 'ARCHIT', 'ORCHID')
OR SUBSTRING(st, pos+2, 1) IN ('T', 'S')
OR ((SUBSTRING(st, pos-1, 1) IN ('A', 'O', 'U', 'E') OR pos = first)
AND SUBSTRING(st, pos+2, 1) IN ('L', 'R', 'N', 'M', 'B', 'H', 'F', 'V', 'W', ' ')) THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSE
IF pos > first THEN
IF SUBSTRING(st, first, 2) = 'MC' THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'K'), pos = pos + 2; END IF;
ELSE
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'X'), pos = pos + 2; END IF;
END IF;
ELSEIF SUBSTRING(st, pos, 2) = 'CZ' AND SUBSTRING(st, pos-2, 4) != 'WICZ' THEN
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'X'), pos = pos + 2; ELSEIF SUBSTRING(st, pos+1, 3) = 'CIA' THEN
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'X'), pos = pos + 3; ELSEIF SUBSTRING(st, pos, 2) = 'CC' AND NOT (pos = (first +1) AND SUBSTRING(st, first, 1) = 'M') THEN
IF SUBSTRING(st, pos+2, 1) IN ('I', 'E', 'H') AND SUBSTRING(st, pos+2, 2) != 'HU' THEN
IF (pos = first +1 AND SUBSTRING(st, first) = 'A') OR
SUBSTRING(st, pos-1, 5) IN ('UCCEE', 'UCCES') THEN
SET pri = CONCAT(pri, 'KS'), sec = CONCAT(sec, 'KS'), pos = pos + 3; ELSE
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'X'), pos = pos + 3; END IF;
ELSE
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; END IF;
ELSEIF SUBSTRING(st, pos, 2) IN ('CK', 'CG', 'CQ') THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSEIF SUBSTRING(st, pos, 2) IN ('CI', 'CE', 'CY') THEN
IF SUBSTRING(st, pos, 3) IN ('CIO', 'CIE', 'CIA') THEN
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'X'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'S'), pos = pos + 2; END IF;
ELSE
IF SUBSTRING(st, pos+1, 2) IN (' C', ' Q', ' G') THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 3; ELSE
IF SUBSTRING(st, pos+1, 1) IN ('C', 'K', 'Q') AND SUBSTRING(st, pos+1, 2) NOT IN ('CE', 'CI') THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSE SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 1; END IF;
END IF;
END IF;
WHEN ch = 'D' THEN
IF SUBSTRING(st, pos, 2) = 'DG' THEN
IF SUBSTRING(st, pos+2, 1) IN ('I', 'E', 'Y') THEN SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'J'), pos = pos + 3; ELSE
SET pri = CONCAT(pri, 'TK'), sec = CONCAT(sec, 'TK'), pos = pos + 2; END IF;
ELSEIF SUBSTRING(st, pos, 2) IN ('DT', 'DD') THEN
SET pri = CONCAT(pri, 'T'), sec = CONCAT(sec, 'T'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'T'), sec = CONCAT(sec, 'T'), pos = pos + 1; END IF;
WHEN ch = 'F' THEN
IF SUBSTRING(st, pos+1, 1) = 'F' THEN
SET pri = CONCAT(pri, 'F'), sec = CONCAT(sec, 'F'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'F'), sec = CONCAT(sec, 'F'), pos = pos + 1; END IF;
WHEN ch = 'G' THEN
IF SUBSTRING(st, pos+1, 1) = 'H' THEN
IF (pos > first AND SUBSTRING(st, pos-1, 1) NOT IN ('A', 'E', 'I', 'O', 'U', 'Y'))
OR ( pos = first AND SUBSTRING(st, pos+2, 1) != 'I') THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSEIF pos = first AND SUBSTRING(st, pos+2, 1) = 'I' THEN
SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'J'), pos = pos + 2; ELSEIF (pos > (first + 1) AND SUBSTRING(st, pos-2, 1) IN ('B', 'H', 'D') )
OR (pos > (first + 2) AND SUBSTRING(st, pos-3, 1) IN ('B', 'H', 'D') )
OR (pos > (first + 3) AND SUBSTRING(st, pos-4, 1) IN ('B', 'H') ) THEN
SET pos = pos + 2; ELSE
IF pos > (first + 2) AND SUBSTRING(st, pos-1, 1) = 'U'
AND SUBSTRING(st, pos-3, 1) IN ('C', 'G', 'L', 'R', 'T') THEN
SET pri = CONCAT(pri, 'F'), sec = CONCAT(sec, 'F'), pos = pos + 2; ELSEIF pos > first AND SUBSTRING(st, pos-1, 1) != 'I' THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSE
SET pos = pos + 1;
END IF;
END IF;
ELSEIF SUBSTRING(st, pos+1, 1) = 'N' THEN
IF pos = (first +1) AND SUBSTRING(st, first, 1) IN ('A', 'E', 'I', 'O', 'U', 'Y') AND NOT is_slavo_germanic THEN
SET pri = CONCAT(pri, 'KN'), sec = CONCAT(sec, 'N'), pos = pos + 2; ELSE
IF SUBSTRING(st, pos+2, 2) != 'EY' AND SUBSTRING(st, pos+1, 1) != 'Y'
AND NOT is_slavo_germanic THEN
SET pri = CONCAT(pri, 'N'), sec = CONCAT(sec, 'KN'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'KN'), sec = CONCAT(sec, 'KN'), pos = pos + 2; END IF;
END IF;
ELSEIF SUBSTRING(st, pos+1, 2) = 'LI' AND NOT is_slavo_germanic THEN
SET pri = CONCAT(pri, 'KL'), sec = CONCAT(sec, 'L'), pos = pos + 2; ELSEIF pos = first AND (SUBSTRING(st, pos+1, 1) = 'Y'
OR SUBSTRING(st, pos+1, 2) IN ('ES', 'EP', 'EB', 'EL', 'EY', 'IB', 'IL', 'IN', 'IE', 'EI', 'ER')) THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'J'), pos = pos + 2; ELSEIF (SUBSTRING(st, pos+1, 2) = 'ER' OR SUBSTRING(st, pos+1, 1) = 'Y')
AND SUBSTRING(st, first, 6) NOT IN ('DANGER', 'RANGER', 'MANGER')
AND SUBSTRING(st, pos-1, 1) not IN ('E', 'I') AND SUBSTRING(st, pos-1, 3) NOT IN ('RGY', 'OGY') THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'J'), pos = pos + 2; ELSEIF SUBSTRING(st, pos+1, 1) IN ('E', 'I', 'Y') OR SUBSTRING(st, pos-1, 4) IN ('AGGI', 'OGGI') THEN
IF SUBSTRING(st, first, 4) IN ('VON ', 'VAN ') OR SUBSTRING(st, first, 3) = 'SCH'
OR SUBSTRING(st, pos+1, 2) = 'ET' THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSE
IF SUBSTRING(st, pos+1, 4) = 'IER ' THEN
SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'J'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'K'), pos = pos + 2; END IF;
END IF;
ELSEIF SUBSTRING(st, pos+1, 1) = 'G' THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 1; END IF;
WHEN ch = 'H' THEN
IF (pos = first OR SUBSTRING(st, pos-1, 1) IN ('A', 'E', 'I', 'O', 'U', 'Y'))
AND SUBSTRING(st, pos+1, 1) IN ('A', 'E', 'I', 'O', 'U', 'Y') THEN
SET pri = CONCAT(pri, 'H'), sec = CONCAT(sec, 'H'), pos = pos + 2; ELSE SET pos = pos + 1; END IF;
WHEN ch = 'J' THEN
IF SUBSTRING(st, pos, 4) = 'JOSE' OR SUBSTRING(st, first, 4) = 'SAN ' THEN
IF (pos = first AND SUBSTRING(st, pos+4, 1) = ' ') OR SUBSTRING(st, first, 4) = 'SAN ' THEN
SET pri = CONCAT(pri, 'H'), sec = CONCAT(sec, 'H'); ELSE
SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'H'); END IF;
ELSEIF pos = first AND SUBSTRING(st, pos, 4) != 'JOSE' THEN
SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'A'); ELSE
IF SUBSTRING(st, pos-1, 1) IN ('A', 'E', 'I', 'O', 'U', 'Y') AND NOT is_slavo_germanic
AND SUBSTRING(st, pos+1, 1) IN ('A', 'O') THEN
SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'H'); ELSE
IF pos = last THEN
SET pri = CONCAT(pri, 'J'); ELSE
IF SUBSTRING(st, pos+1, 1) not IN ('L', 'T', 'K', 'S', 'N', 'M', 'B', 'Z')
AND SUBSTRING(st, pos-1, 1) not IN ('S', 'K', 'L') THEN
SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'J'); END IF;
END IF;
END IF;
END IF;
IF SUBSTRING(st, pos+1, 1) = 'J' THEN
SET pos = pos + 2;
ELSE
SET pos = pos + 1;
END IF;
WHEN ch = 'K' THEN
IF SUBSTRING(st, pos+1, 1) = 'K' THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 1; END IF;
WHEN ch = 'L' THEN
IF SUBSTRING(st, pos+1, 1) = 'L' THEN
IF (pos = (last - 2) AND SUBSTRING(st, pos-1, 4) IN ('ILLO', 'ILLA', 'ALLE'))
OR ((SUBSTRING(st, last-1, 2) IN ('AS', 'OS') OR SUBSTRING(st, last) IN ('A', 'O'))
AND SUBSTRING(st, pos-1, 4) = 'ALLE') THEN
SET pri = CONCAT(pri, 'L'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'L'), sec = CONCAT(sec, 'L'), pos = pos + 2; END IF;
ELSE
SET pri = CONCAT(pri, 'L'), sec = CONCAT(sec, 'L'), pos = pos + 1; END IF;
WHEN ch = 'M' THEN
IF SUBSTRING(st, pos-1, 3) = 'UMB'
AND (pos + 1 = last OR SUBSTRING(st, pos+2, 2) = 'ER')
OR SUBSTRING(st, pos+1, 1) = 'M' THEN
SET pri = CONCAT(pri, 'M'), sec = CONCAT(sec, 'M'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'M'), sec = CONCAT(sec, 'M'), pos = pos + 1; END IF;
WHEN ch = 'N' THEN
IF SUBSTRING(st, pos+1, 1) = 'N' THEN
SET pri = CONCAT(pri, 'N'), sec = CONCAT(sec, 'N'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'N'), sec = CONCAT(sec, 'N'), pos = pos + 1; END IF;
WHEN ch = 'P' THEN
IF SUBSTRING(st, pos+1, 1) = 'H' THEN
SET pri = CONCAT(pri, 'F'), sec = CONCAT(sec, 'F'), pos = pos + 2; ELSEIF SUBSTRING(st, pos+1, 1) IN ('P', 'B') THEN SET pri = CONCAT(pri, 'P'), sec = CONCAT(sec, 'P'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'P'), sec = CONCAT(sec, 'P'), pos = pos + 1; END IF;
WHEN ch = 'Q' THEN
IF SUBSTRING(st, pos+1, 1) = 'Q' THEN
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'K'), sec = CONCAT(sec, 'K'), pos = pos + 1; END IF;
WHEN ch = 'R' THEN
IF pos = last AND not is_slavo_germanic
AND SUBSTRING(st, pos-2, 2) = 'IE' AND SUBSTRING(st, pos-4, 2) NOT IN ('ME', 'MA') THEN
SET sec = CONCAT(sec, 'R'); ELSE
SET pri = CONCAT(pri, 'R'), sec = CONCAT(sec, 'R'); END IF;
IF SUBSTRING(st, pos+1, 1) = 'R' THEN
SET pos = pos + 2;
ELSE
SET pos = pos + 1;
END IF;
WHEN ch = 'S' THEN
IF SUBSTRING(st, pos-1, 3) IN ('ISL', 'YSL') THEN
SET pos = pos + 1;
ELSEIF pos = first AND SUBSTRING(st, first, 5) = 'SUGAR' THEN
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'S'), pos = pos + 1; ELSEIF SUBSTRING(st, pos, 2) = 'SH' THEN
IF SUBSTRING(st, pos+1, 4) IN ('HEIM', 'HOEK', 'HOLM', 'HOLZ') THEN
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'S'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'X'), pos = pos + 2; END IF;
ELSEIF SUBSTRING(st, pos, 3) IN ('SIO', 'SIA') OR SUBSTRING(st, pos, 4) = 'SIAN' THEN
IF NOT is_slavo_germanic THEN
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'X'), pos = pos + 3; ELSE
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'S'), pos = pos + 3; END IF;
ELSEIF (pos = first AND SUBSTRING(st, pos+1, 1) IN ('M', 'N', 'L', 'W')) OR SUBSTRING(st, pos+1, 1) = 'Z' THEN
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'X'); IF SUBSTRING(st, pos+1, 1) = 'Z' THEN
SET pos = pos + 2;
ELSE
SET pos = pos + 1;
END IF;
ELSEIF SUBSTRING(st, pos, 2) = 'SC' THEN
IF SUBSTRING(st, pos+2, 1) = 'H' THEN
IF SUBSTRING(st, pos+3, 2) IN ('OO', 'ER', 'EN', 'UY', 'ED', 'EM') THEN
IF SUBSTRING(st, pos+3, 2) IN ('ER', 'EN') THEN
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'SK'), pos = pos + 3; ELSE
SET pri = CONCAT(pri, 'SK'), sec = CONCAT(sec, 'SK'), pos = pos + 3; END IF;
ELSE
IF pos = first AND SUBSTRING(st, first+3, 1) not IN ('A', 'E', 'I', 'O', 'U', 'Y') AND SUBSTRING(st, first+3, 1) != 'W' THEN
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'S'), pos = pos + 3; ELSE
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'X'), pos = pos + 3; END IF;
END IF;
ELSEIF SUBSTRING(st, pos+2, 1) IN ('I', 'E', 'Y') THEN
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'S'), pos = pos + 3; ELSE
SET pri = CONCAT(pri, 'SK'), sec = CONCAT(sec, 'SK'), pos = pos + 3; END IF;
ELSEIF pos = last AND SUBSTRING(st, pos-2, 2) IN ('AI', 'OI') THEN
SET sec = CONCAT(sec, 'S'), pos = pos + 1; ELSE
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'S'); IF SUBSTRING(st, pos+1, 1) IN ('S', 'Z') THEN
SET pos = pos + 2;
ELSE
SET pos = pos + 1;
END IF;
END IF;
WHEN ch = 'T' THEN
IF SUBSTRING(st, pos, 4) = 'TION' THEN
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'X'), pos = pos + 3; ELSEIF SUBSTRING(st, pos, 3) IN ('TIA', 'TCH') THEN
SET pri = CONCAT(pri, 'X'), sec = CONCAT(sec, 'X'), pos = pos + 3; ELSEIF SUBSTRING(st, pos, 2) = 'TH' OR SUBSTRING(st, pos, 3) = 'TTH' THEN
IF SUBSTRING(st, pos+2, 2) IN ('OM', 'AM') OR SUBSTRING(st, first, 4) IN ('VON ', 'VAN ')
OR SUBSTRING(st, first, 3) = 'SCH' THEN
SET pri = CONCAT(pri, 'T'), sec = CONCAT(sec, 'T'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, '0'), sec = CONCAT(sec, 'T'), pos = pos + 2; END IF;
ELSEIF SUBSTRING(st, pos+1, 1) IN ('T', 'D') THEN
SET pri = CONCAT(pri, 'T'), sec = CONCAT(sec, 'T'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'T'), sec = CONCAT(sec, 'T'), pos = pos + 1; END IF;
WHEN ch = 'V' THEN
IF SUBSTRING(st, pos+1, 1) = 'V' THEN
SET pri = CONCAT(pri, 'F'), sec = CONCAT(sec, 'F'), pos = pos + 2; ELSE
SET pri = CONCAT(pri, 'F'), sec = CONCAT(sec, 'F'), pos = pos + 1; END IF;
WHEN ch = 'W' THEN
IF SUBSTRING(st, pos, 2) = 'WR' THEN
SET pri = CONCAT(pri, 'R'), sec = CONCAT(sec, 'R'), pos = pos + 2; ELSEIF pos = first AND (SUBSTRING(st, pos+1, 1) IN ('A', 'E', 'I', 'O', 'U', 'Y')
OR SUBSTRING(st, pos, 2) = 'WH') THEN
IF SUBSTRING(st, pos+1, 1) IN ('A', 'E', 'I', 'O', 'U', 'Y') THEN
SET pri = CONCAT(pri, 'A'), sec = CONCAT(sec, 'F'), pos = pos + 1; ELSE
SET pri = CONCAT(pri, 'A'), sec = CONCAT(sec, 'A'), pos = pos + 1; END IF;
ELSEIF (pos = last AND SUBSTRING(st, pos-1, 1) IN ('A', 'E', 'I', 'O', 'U', 'Y'))
OR SUBSTRING(st, pos-1, 5) IN ('EWSKI', 'EWSKY', 'OWSKI', 'OWSKY')
OR SUBSTRING(st, first, 3) = 'SCH' THEN
SET sec = CONCAT(sec, 'F'), pos = pos + 1; ELSEIF SUBSTRING(st, pos, 4) IN ('WICZ', 'WITZ') THEN
SET pri = CONCAT(pri, 'TS'), sec = CONCAT(sec, 'FX'), pos = pos + 4; ELSE SET pos = pos + 1;
END IF;
WHEN ch = 'X' THEN
IF not(pos = last AND (SUBSTRING(st, pos-3, 3) IN ('IAU', 'EAU')
OR SUBSTRING(st, pos-2, 2) IN ('AU', 'OU'))) THEN
SET pri = CONCAT(pri, 'KS'), sec = CONCAT(sec, 'KS'); END IF;
IF SUBSTRING(st, pos+1, 1) IN ('C', 'X') THEN
SET pos = pos + 2;
ELSE
SET pos = pos + 1;
END IF;
WHEN ch = 'Z' THEN
IF SUBSTRING(st, pos+1, 1) = 'H' THEN
SET pri = CONCAT(pri, 'J'), sec = CONCAT(sec, 'J'), pos = pos + 1; ELSEIF SUBSTRING(st, pos+1, 3) IN ('ZO', 'ZI', 'ZA')
OR (is_slavo_germanic AND pos > first AND SUBSTRING(st, pos-1, 1) != 'T') THEN
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'TS'); ELSE
SET pri = CONCAT(pri, 'S'), sec = CONCAT(sec, 'S'); END IF;
IF SUBSTRING(st, pos+1, 1) = 'Z' THEN
SET pos = pos + 2;
ELSE
SET pos = pos + 1;
END IF;
ELSE
SET pos = pos + 1; END CASE;
IF pos = prevpos THEN
SET pos = pos +1;
SET pri = CONCAT(pri,'<didnt incr>'); END IF;
END WHILE;
IF pri != sec THEN
SET pri = CONCAT(pri, ';', sec);
END IF;
RETURN (pri);
END
I also needed a levenshtein function and managed to sort out one of those, but performance was such (when comparing values from many rows) that it was far faster to read the rows and use the php built in levenshtein function.

MySQL Update multiple rows on a single column based on values from that same column

I have a table that looks like the following:
ID Key Value Order
1 gender m 0
2 gender f 0
34 age 10 0
35 age 80 0
To update these rows I have to use the following:
UPDATE `DemoGroup` SET `value` = 'male' WHERE `value` = 'm'
UPDATE `DemoGroup` SET `value` = 'female' WHERE `value` = 'f'
UPDATE `DemoGroup` SET `value` = '10-19' WHERE `value` = '10'
UPDATE `DemoGroup` SET `value` = '80-89' WHERE `value` = '80'
Is there a way to consolidate this into one update statement, without using the ID (which is not guaranteed to be the same), such as (even though this won't work)...
UPDATE `DemoGroup`
SET `value`= CASE `value`
WHEN 'm' THEN 'male',
WHEN 'f' THEN 'female' END
WHERE `value` = 'm' OR `value` = 'f'
Even more of a bonus (but not nessesary) is if I could figure out how to set the Order field as well for each row...
You should probably update the values based not only on the value of value but on the value of key, otherwise you could update 'm' to 'male' when key is 'shirt-size'.
UPDATE `DemoGroup`
SET `value` = CASE
WHEN (`key`, `value`) = ('gender', 'm') THEN 'male'
WHEN (`key`, `value`) = ('gender', 'f') THEN 'female'
WHEN (`key`, `value`) = ('age', '10') THEN '10-19'
WHEN (`key`, `value`) = ('age', '80') THEN '80-89'
ELSE `value` -- no-op for other values
END
WHERE `key` IN ('gender','age');
MySQL does have a case function. Try:
UPDATE DemoGroup SET `value` =
CASE `value` WHEN 'm' THEN 'male'
WHEN 'f' THEN 'female'
WHEN '10' THEN '10-19'
WHEN '80' THEN '80-89' END;