charlie charlie - 6 months ago 7
SQL Question

SQL query to select from two tables and return if a value exists

i have two tables:

callcosts
callcosts_custom


is there a way i can select from both tables and return the value of
cost
from callcosts_custom if the row exists and if it doesnt exist, return the column
price
from
callcosts


with the following
WHERE
clause:

WHERE callcosts_custom.parent = callcosts.sequence


basically, the
callcosts
table has all of the default data in and custom data can be added per customer.

the relationship is as follows:

call_costs.sequence = callcosts_custom.parent


so i want to check if a row exists in callcosts_custom for a specific
callcosts_custom.customer
. if it does, it will return
callcosts_custom.cost
and if it does not exist to return
callcosts_price


updated query:

select b.cost


from call_costs_custom b
where a.sequence = b.parent AND b.customer_seq = '124'
union all
select a.retail
from call_costs a
where a.sequence = '4706' and
not exists (select 1 from call_costs_custom b where b.parent = a.sequence);

Answer

Yes . . . this sounds like a prioritization query. This typically looks like

select ccc.price
from callcosts_custom ccc
where ccc.?? = ??
union all
select cc.price
from callcosts ccc
where cc.?? = ?? and
      not exists (select 1 from callcosts_custom ccc where ccc.?? = cc.??);

It is unclear from your question what the columns are.

Comments