Skip to content

Instantly share code, notes, and snippets.

@w495
Last active August 29, 2015 14:12
Show Gist options
  • Save w495/b41fcdfab9a0beec3e4b to your computer and use it in GitHub Desktop.
Save w495/b41fcdfab9a0beec3e4b to your computer and use it in GitHub Desktop.
Пример того, как можно конструировать SQL запросы во время выполнения. Скрипт делает выборку из шардированной базы данных. Выборка выполняется за один запрос.
set session group_concat_max_len = 9223372036854775807;
set @expr = (
select
group_concat(
concat(
'C.Expression REGEXP "P',PatternID, '[^0-9]"'
)
SEPARATOR ' or '
)
from
Pattern
where
PatternID in (
174,175,176,177,198,200,209,216,217,220,229,238,254,255,259,276,281
)
);
set @state = (
select
group_concat(
concat('(',
'select ',
B.No, ' as Shard, ',
'B.BannerID, ',
'B.EngineID ',
'from ',
'Banner', B.No, ' as B ',
'join ',
'Context', B.No, ' as C ',
'on ',
'C.ContextID = B.ContextID ',
'and B.EngineID not in (0, 67) ',
'and (', @expr ,') ',
'order by ',
'B.EngineID, B.BannerID',
')')
SEPARATOR ' union '
)
from (
select
distinct BaseNo as No
from
OrderInfo
order by
BaseNo
) as B
);
prepare stmt1 from @state;
execute stmt1;
deallocate prepare stmt1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment