2. MySQL. YourTable — это ваша таблица с числом строк более 100.
/* Program by Evgeny.Stepanischev*/
SET @beer:=100;
SET @w:=' on the wall';
SELECT IF(@beer<2,'\nNo more bottles of beer...\n\
Go to the store and buy some more...\n99 bottles of \
beer.',CONCAT(CONCAT('\n',@str:=CONCAT(@beer:=@beer-1,\
@bot:=CONCAT(' bottle',IF (@beer>1,'s',''),' of beer')),\
@w,', ',@str,'.\nTake one and pass it around,\n'), IF \
(@beer-1=0,'No more ',@beer-1),@bot,@w,'.')) as beer
FROM YourTable LIMIT 100;
1. PostgreSQL. YourTable — это ваша таблица с числом строк более 100.
-- Written by Evgeny.Stepanischev
--
CREATE SEQUENCE temp_beer_seq INCREMENT -1 MINVALUE 0 MAXVALUE 99 START 99;
CREATE FUNCTION temp_b() RETURNS text
AS 'SELECT \' bottle\'||CASE WHEN last_value=1 THEN
\'\'::text ELSE \'s\'::text END||\' of beer\' FROM temp_beer_seq'
LANGUAGE 'sql';
CREATE FUNCTION temp_b_out() RETURNS text
AS 'SELECT s.last_value||temp_b()||\' on the wall, \'||
s.last_value||temp_b()||\'.\\n Take one and pass it around,\\n \'||
CASE WHEN NEXTVAL(\'temp_beer_seq\')=0 THEN \'No\'
ELSE text(s.last_value-0) END||
temp_b()||\' on the wall.\\n\'
FROM temp_beer_seq s'
LANGUAGE 'sql';
SELECT CASE WHEN temp_beer_seq.last_value=0
THEN 'No more bottles of beer...\n Go to the store and buy some more...\n
99 bottles of beer.' ELSE temp_b_out() END
FROM YourTable LIMIT 100,1;
--
DROP SEQUENCE temp_beer_seq;
DROP FUNCTION temp_b();
DROP FUNCTION temp_b_out();
-- END