这是我刚刚进行的查询,它工作正常
select
sum(IF(l.bandera='DEBITO',l.valor,0)) totdebito,
sum(IF(l.bandera='CREDITO',l.valor,0)) totcredito ,n.nit_ID
from lineaasiento l left join clientesam c on c.Dk=l.clientesmayor_OID
left join proveedores p on p.Dk=l.proveedor_OID left join empleado e on e.Dk=l.empleado_OID
left join banco b on b.Dk=l.banco_OID left join cuentacontable cc on cc.Dk=l.cuentacontable_OID
left join nit n on n.Dk=coalesce(c.nit_OID,p.nit_OID,e.nit_OID,b.nit_OID,null)
where (timefcontable between '2013-01-01' and '2013-12-31') and
estadolinea_OID in (Select Dk from estadoasiento where codigo!=4) and
cc.cuentacontable_ID REGEXP '^5'
group by n.Dk
但是,我现在有一个问题...我需要 REGEX '^5' 是这样的子查询句子
select
sum(IF(l.bandera='DEBITO',l.valor,0)) totdebito,
sum(IF(l.bandera='CREDITO',l.valor,0)) totcredito ,n.nit_ID
from lineaasiento l left join clientesam c on c.Dk=l.clientesmayor_OID
left join proveedores p on p.Dk=l.proveedor_OID left join empleado e on e.Dk=l.empleado_OID
left join banco b on b.Dk=l.banco_OID left join cuentacontable cc on cc.Dk=l.cuentacontable_OID
left join nit n on n.Dk=coalesce(c.nit_OID,p.nit_OID,e.nit_OID,b.nit_OID,null)
where (timefcontable between '2013-01-01' and '2013-12-31') and
estadolinea_OID in (Select Dk from estadoasiento where codigo!=4) and
cc.cuentacontable_ID REGEXP (^a query that returns a lot of values and i need to get the elements that starts by this)
group by n.Dk
可能吗?
最佳答案
用于构建您的正则表达式...
select
'^(' || group_concat(my_value separator '|') || ')' as regex
from
my_table
注意:group_concat
返回的结果有 1024 字节的限制。因此,如果您期望更大的结果,请在最终查询之前运行以下查询(见下文)。
set group_concat_max_len=2048
<小时/>
最终查询
select
sum(IF(l.bandera='DEBITO',l.valor,0)) totdebito,
sum(IF(l.bandera='CREDITO',l.valor,0)) totcredito ,
n.nit_ID
from
lineaasiento l
left join
clientesam c on c.Dk=l.clientesmayor_OID
left join
proveedores p on p.Dk=l.proveedor_OID
left join
empleado e on e.Dk=l.empleado_OID
left join
banco b on b.Dk=l.banco_OID
left join
cuentacontable cc on cc.Dk=l.cuentacontable_OID
left join
nit n on n.Dk=coalesce(c.nit_OID, p.nit_OID, e.nit_OID, b.nit_OID, null)
inner join
(select '^(' || group_concat(my_value separator '|') || ')' as regex from my_table) values
where
(
timefcontable between '2013-01-01' and '2013-12-31'
)
and estadolinea_OID in (
Select
Dk
from
estadoasiento
where
codigo!=4
)
and cc.cuentacontable_ID REGEXP values.regex
group by
n.Dk
关于mysql - REGEX 由子查询开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21587535/