Quantcast
Channel: MySQL - Column value depends on values from two different tables - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by kevinnwhat for MySQL - Column value depends on values from two different tables

$
0
0

With my original design, this view is simple to implement and it performs well. But if I get rid of the column TableB.price, how would I implement this view

create table TableA (id int,v_time date,price int,special_price int);create table TableB (table_a_id int,v_date date);create table SpecialDate (v_date date,description varchar(5));insert into TableA values (1,'2019-09-01', 10, 5);insert into TableB values (1,'2019-10-01');insert into TableB values (1,'2019-11-01');insert into TableA values (2,'2019-12-01', 50, 1);insert into TableB values (2,'2019-12-02');insert into TableB values (2,'2019-12-03');insert into TableB values (2,'2019-11-01');insert into SpecialDate values('2019-11-01', 'sale');--your view definitionselect a.id,   a.v_time,   count(*) as count_of_table_b_instances,   sum(      case     when s.v_date is not null then a.special_price     else a.price     end   ) as sum_of_table_b from TableA a join TableB b   on a.id = b.table_a_id left join SpecialDate s  on b.v_date = s.v_date group by a.id,      a.v_time

Will the performance take a hit?

I can't say for sure because I can't assume you have the right indexes setup to support this query without seeing the DDL of the tables. If you have indexes in place to support the join clauses, or if you don't have much data in these tables, I don't think you would take a performance hit.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>