Category

Routines/procedures: queries with conditions

Допустим у вас есть входящий параметр "ModerationStatus" который отвечает за фильтр результатов выборки. В зависимости от этого параметра вам нужно показывать или все строки из таблицы или только некоторые.
Вот пример того, как это можно сделать с наименьшими усилиями:

CREATE PROCEDURE `Photo_Get_List`(ModerationStatus tinyint(1))
BEGIN
SET @query = 'SELECT * FROM Photos';
IF ModerationStatus IS NOT NULL THEN
SET @query = CONCAT(@query, ' WHERE ModerationStatus = ', ModerationStatus);
END IF;
PREPARE statement FROM @query;
EXECUTE statement;
DEALLOCATE PREPARE statement;
END;

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Это простая проверка на то, что со мной общается человек, а не глупая машина. Умные машины тоже не приветствуются :)
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.
© 2008-2009. Konstantin Artemov