Hi
I've been working on a database which basically incorporates 3 tables to
describe say a widget which is either sold or leased.
I have the Widget table which stores the information related to the
widget itself.
I then have a WidgetSale table which stores only information related to
the sale of the widget (advertised price, headline, copy, date of sale
etc) if it is currently for sale.
Finally I have the WidgetLease table which stores only information on
the lease of the widget (cost of lease, headline, copy, when current
lease is up, etc) if it is available for lease/loan.
A widget may be available for sale and for lease at the same time, in
which case it will have entries in all 3 tables.
When it comes to searching, the searcher may want to include both
widgets available for sale and for lease - as such the one widget should
be returned twice - once with the details for sale and once with the
details for lease. The results are sorted as well so the two entries
may appear in well apart from each other in the result set. This was
the main reason behind separating the tables in the first place.
So basically I create a union of two SQL queries - one which pulls the
same details and one which pulls the lease details. Because the fields
in both queries need to match I select the common fields from the Widget
table first and then for the sale table I select the appropriate sale
fields, then nulls for each field which will come from the lease table.
In the lease query I do the reverse so effectively I end up with -
select w.field1,w.fiel d2,w.field3,ws. sale1,ws.sale2, null as
'lease1',null as 'lease2'
from widgets w inner join widgetsale ws on w.widgetid=ws.w idgetid
union
select w.field1,w.fiel d2,w.field3,nul l as 'sale1',null as
'sale2',wl.leas e1,wl.lease2
from widgets w inner join widgetlease ws on w.widgetid=wl.w idgetid
Now various criteria can be specified for the widget search which means
both the queries have a where clause. They may also do something like
'widgets for sale between $x and $y or for lease between $a and $b'
I'm wondering is this the best way to achieve it and how efficient is it
assuming that potentially there could be hundreds of thousands of
widgets ?
I'd also considered using a pivot table with 3 columns - widgetid,
saleid and leaseid and then joining the widget table to the pivot table
and from there a left outer join to the sale and lease tables. So for a
widget on both sale and lease I'd end up with two rows in the pivot
table - one with a null saleid and leaseid=widgeti d and the other row
with saleid=widgetid and leaseid=null so I'd end up with two records
returned
Thanks
Jody
I've been working on a database which basically incorporates 3 tables to
describe say a widget which is either sold or leased.
I have the Widget table which stores the information related to the
widget itself.
I then have a WidgetSale table which stores only information related to
the sale of the widget (advertised price, headline, copy, date of sale
etc) if it is currently for sale.
Finally I have the WidgetLease table which stores only information on
the lease of the widget (cost of lease, headline, copy, when current
lease is up, etc) if it is available for lease/loan.
A widget may be available for sale and for lease at the same time, in
which case it will have entries in all 3 tables.
When it comes to searching, the searcher may want to include both
widgets available for sale and for lease - as such the one widget should
be returned twice - once with the details for sale and once with the
details for lease. The results are sorted as well so the two entries
may appear in well apart from each other in the result set. This was
the main reason behind separating the tables in the first place.
So basically I create a union of two SQL queries - one which pulls the
same details and one which pulls the lease details. Because the fields
in both queries need to match I select the common fields from the Widget
table first and then for the sale table I select the appropriate sale
fields, then nulls for each field which will come from the lease table.
In the lease query I do the reverse so effectively I end up with -
select w.field1,w.fiel d2,w.field3,ws. sale1,ws.sale2, null as
'lease1',null as 'lease2'
from widgets w inner join widgetsale ws on w.widgetid=ws.w idgetid
union
select w.field1,w.fiel d2,w.field3,nul l as 'sale1',null as
'sale2',wl.leas e1,wl.lease2
from widgets w inner join widgetlease ws on w.widgetid=wl.w idgetid
Now various criteria can be specified for the widget search which means
both the queries have a where clause. They may also do something like
'widgets for sale between $x and $y or for lease between $a and $b'
I'm wondering is this the best way to achieve it and how efficient is it
assuming that potentially there could be hundreds of thousands of
widgets ?
I'd also considered using a pivot table with 3 columns - widgetid,
saleid and leaseid and then joining the widget table to the pivot table
and from there a left outer join to the sale and lease tables. So for a
widget on both sale and lease I'd end up with two rows in the pivot
table - one with a null saleid and leaseid=widgeti d and the other row
with saleid=widgetid and leaseid=null so I'd end up with two records
returned
Thanks
Jody
Comment