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 Tully Clark for MySQL - Column value depends on values from two different tables

$
0
0

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_dateFROM    table_a    left join table_b on table_a.id = table_b.table_a_idWHERE    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_dateFROM    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_idWHERE    NOW() between start_date and end_date

Viewing all articles
Browse latest Browse all 3

Trending Articles