解析一个字符串,并在子查询MySQL的谓词中使用已解析的值

时间:2016-01-08 21:33:20

标签: mysql

我有一个包含symbol属性的表。我想在symbol值的某个位置解析我期望的某组字母的symbol属性,并将它们与从子查询返回的解析值进行比较。这是我到目前为止的查询:

select * from market_instrument inner join exchange_instrument on market_instrument.id = exchange_instrument.instrument_id 
inner join Table1 on market_instrument.id = Table1.instrument_id 
left join Table2 on market_instrument.id = Table2.instrument_id
left join `options`on market_instrument.id = `options`.instrument_id 
left join Table3 on market_instrument.id = Table3.instrument_id
where symbol in (select symbol from market_instrument inner join fills on market_instrument.id = fills.instrument_id);

这样做会将外部查询的symbol属性与内部查询中的symbol属性进行比较。 symbol中的值如下所示:

1FU_PLAT_1610

我只想比较该值的一个子集。我可以做...where symbol[1:5] in (select symbol[1:5] from market_instrument inner join fills on market_instrument.id = fills.instrument_id);之类的事情吗?

1 个答案:

答案 0 :(得分:2)

使用SQL substring()函数:

select * from market_instrument inner join exchange_instrument on market_instrument.id = exchange_instrument.instrument_id 
inner join Table1 on market_instrument.id = Table1.instrument_id 
left join Table2 on market_instrument.id = Table2.instrument_id
left join `options`on market_instrument.id = `options`.instrument_id 
left join Table3 on market_instrument.id = Table3.instrument_id
where substring(symbol, 1, 5) in (select substring(symbol, 1, 5) from market_instrument inner join fills on market_instrument.id = fills.instrument_id);