sql - Ensuring two columns only contain valid results from same subquery -


i have following table:

id   symbol_01   symbol_02 1    abc         xyz 2    kjh         okd 3    que         qid 

i need query ensures symbol_01 , symbol_02 both contained in list of valid symbols. in other words needs this:

select *  mytable symbol_01 in (     select valid_symbols      somewhere) , symbol_02 in (     select valid_symbols      somewhere) 

the above example work correctly, subquery used determine list of valid symbols identical both times , quite large. innefficient run twice in example.

is there way without duplicating 2 identical sub queries?

you try use cte like;

with validsymbols (     select distinct valid_symbol     somewhere ) select mt.*  mytable mt inner join validsymbols v1     on mt.symbol_01 = v1.valid_symbol inner join validsymbols v2     on mt.symbol_02 = v2.valid_symbol 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -