MySQL - Column value depends on values from two different tables

by ibrahim mahrir   Last Updated October 10, 2019 00:06 AM - source

I have a MySQL database that contains these tables (* denotes primary key):

TableA:

----------------------------------------------
|  *id  |  time  |  price  |  special_price  |
----------------------------------------------

TableB:

-------------------------------------
|  *table_a_id  |  *date  |  price  |
-------------------------------------

SpecialDate:

--------------------------
|  *date  |  description |
--------------------------

The value of TableB.price column of each entry in TableB will be either TableA.price or TableA.special_price of the corresponding TableA entry, depending on whether TableB.date is a special date or not (whether TableB.date exists in the SpecialDate table).

This design, as it stands, doesn't feel right as every time I'm adding a record to TableB, I have to look up the date from SpecialDate to see if it's a special date or not, then look up the corresponding entry from TableA to get the value for the price column. And every time a record from SpecialDate gets added or updated, I have to go over every entry in TableB to update its price column.

I'm thinking the column TableB.price should be removed and calculated when needed but I'm not sure how to do that using SQL. I have a lot of views that depend on that price column, for example:

TableAFullView:

-----------------------------------------------------------------------------
|  id  |  time  |  count_of_table_b_instances  |  sum_of_table_b_instances  |
-----------------------------------------------------------------------------

Where count_of_table_b_instances is the number of TableB entries that point to this TableA entry and sum_of_table_b_instances is the sum of their prices.

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? Will the performance take a hit?

Note: Feel free to edit the title as I don't know how to best describe this problem.



Answers 1


The simplest set up is probably something like this:

Table A

-----------------------------
|  *id  |  time  |  price  |  
-----------------------------

Table B

------------------------------------------------------------
|  *id  |  table_a_id(f)  | start_date | end_date | price  |  
------------------------------------------------------------

Which you can then query like this:

SELECT
    table_a.id
    , time
    , coalesce(table_b.price, table_a.price) as price
    , start_date
    , end_date
FROM
    table_a
    left join table_b on table_a.id = table_b.table_a_id
WHERE
    NOW() between start_date and end_date

If you want to 'group' special's together, you could do this:

Table A

-----------------------------
|  *id  |  time  |  price  |  
-----------------------------

Table B

------------------------------------------------------------
|  *id  |  table_a_id(f)  | table_c_id(f) | price  |  
------------------------------------------------------------

Table C

------------------------------------------------------------
|  *id  |  details | start_date | end_date |
------------------------------------------------------------

And query it like:

SELECT
    table_a.id
    , time
    , table_c.details
    , coalesce(table_b.price, table_a.price) as price
    , start_date
    , end_date
FROM
    table_a
    left join table_b on table_a.id = table_b.table_a_id
    left join table_c on table_c.id = table_b.table_c_id
WHERE
    NOW() between start_date and end_date
Tully Clark
Tully Clark
October 09, 2019 23:55 PM

Related Questions



Need a table schema for general store app

Updated March 27, 2015 07:02 AM

Are junction tables a good practice?

Updated July 06, 2015 13:02 PM

Is my data 'Big Data'?

Updated June 03, 2015 22:02 PM