Category: database
-
Postgres – Hamming distance in plpython
CREATE OR REPLACE FUNCTION util.hamming_distance (s1 text, s2 text) RETURNS integer /* select * from util.hamming_distance (‘hella3’, ‘hillo2’) */ AS $$ return sum([ch1 != ch2 for ch1, ch2 in zip(s1, s2)]) $$ LANGUAGE plpythonu;
-
MySQL – Append text data in column
UPDATE mytable SET mycolumn = CONCAT(mycolumn, ‘this is my appended text’) where id=1234;
-
MySQL – Sorting records/users based on closeness to a age in SQL
Use: sort by users whose age is closest to a defined age. In the example below, 25 years. User Age 1 25 3 23 4 26 5 27 Idea: select abs(u_age-25) as age_difference, u_age from users order by age_difference, u_age; The MySQL function abs ensures that the subtracted result is always positive. The date of…