sql - Mysql variable inside a long query -


i think have problem variable inside mysql query on mysql 5.6:

select distinct trim(trailing '.' merge_s.rdata) `content`,             '120' ttl,             `merge_s`.`pri` `prio`,             (select type types type = @type:='mx') `type`,             (select id soa concat(substring_index((select @rr:='mina.net'),'.',-2),'.')=soa.origin) domain_id,             `merge_s`.`rr` `rr`     (`merge_s` left join `merge_s` `db2` on             (((`merge_s`.`rr` = `db2`.`rr`) , (`merge_s`.`pri` < `db2`.`pri`) , (`merge_s`.`type` = `db2`.`type`))))     ((`merge_s`.`status` = '1') or (`merge_s`.`type` = 'ns') or (`merge_s`.`type` = 'soa')) ,             (isnull(`db2`.`pri`) or (`merge_s`.`type` = 'mx')) ,             (merge_s.type=@type , merge_s.rr=@rr) 

the query returns correct without rows. same query, replace hands last row (merge_s.type=@type , merge_s.rr=@rr) aspects in variable returns right 2 rows

select distinct trim(trailing '.' merge_s.rdata) `content`,             '120' ttl,             `merge_s`.`pri` `prio`,             (select type types type = @type:='mx') `type`,             (select id soa concat(substring_index((select @rr:='mina.net'),'.',-2),'.')=soa.origin) domain_id,             `merge_s`.`rr` `rr`     (`merge_s` left join `merge_s` `db2` on             (((`merge_s`.`rr` = `db2`.`rr`) , (`merge_s`.`pri` < `db2`.`pri`) , (`merge_s`.`type` = `db2`.`type`))))     ((`merge_s`.`status` = '1') or (`merge_s`.`type` = 'ns') or (`merge_s`.`type` = 'soa')) ,             (isnull(`db2`.`pri`) or (`merge_s`.`type` = 'mx')) ,             (merge_s.type='mx' , merge_s.rr='mina.net') 

the 2 rows:

content         ttl     prio    type    domain_id   rr   mail.mina.net   120     50       mx       3         mina.net mail.tes.net    120     60       mx       3         mina.net 

in query, variables being set in subqueries in select clause:

select distinct trim(trailing '.' merge_s.rdata) `content`,        '120' ttl, `merge_s`.`pri` `prio`,         (select type types type = @type:='mx') `type`,         (select id          soa          concat(substring_index((select @rr:='mina.net'),'.',-2),'.')=soa.origin         ) domain_id,         `merge_s`.`rr` `rr` `merge_s` left join      `merge_s` `db2`       on(((`merge_s`.`rr` = `db2`.`rr`) , (`merge_s`.`pri` < `db2`.`pri`) , (`merge_s`.`type` = `db2`.`type`)))) ((`merge_s`.`status` = '1') or (`merge_s`.`type` = 'ns') or (`merge_s`.`type` = 'soa')) ,       (isnull(`db2`.`pri`) or (`merge_s`.`type` = 'mx')) ,       (merge_s.type=@type , merge_s.rr=@rr); 

what have discovered lot of processing happens in sql statement before such clauses executed. in general, from clause first clause executed , select 1 of last. put variable declarations in main from clause using cross join. version should work:

select distinct trim(trailing '.' merge_s.rdata) `content`,        '120' ttl, `merge_s`.`pri` `prio`,         (select type types type = @type) `type`,         (select id          soa          concat(substring_index( @rr,'.',-2),'.')=soa.origin         ) domain_id,         `merge_s`.`rr` `rr` `merge_s` left join      `merge_s` `db2`       on(((`merge_s`.`rr` = `db2`.`rr`) , (`merge_s`.`pri` < `db2`.`pri`) ,           (`merge_s`.`type` = `db2`.`type`)))) cross join       (select @type:='mx', @rr:='mina.net') const ((`merge_s`.`status` = '1') or (`merge_s`.`type` = 'ns') or (`merge_s`.`type` = 'soa')) ,       (isnull(`db2`.`pri`) or (`merge_s`.`type` = 'mx')) ,       (merge_s.type=@type , merge_s.rr=@rr); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -