Hi people,
suffering from from an ordering dilemma in which using three queries (on same table) using Union all presents data in order in which the queries are executed and yet gives duplicates, presents the data in an ambiguous order format when using it as a sub query.
Here is the basic union query
Select * from products where
( BrandName Like '%fish%' AND BrandName Like '%oil%')
AND
( Keywords Like '%fish%' AND Keywords Like '%oil%')
AND
DisplayOnWeb = 1
union all
Select * from products where
( BrandName Like '%fish%' AND BrandName Like '%oil%')
or
( Keywords Like '%fish%' AND Keywords Like '%oil%')
AND
DisplayOnWeb = 1
union all
Select * from products where
( BrandName Like '%fish%' or BrandName Like '%oil%')
or
( Keywords Like '%fish%' or Keywords Like '%oil%')
AND DisplayOnWeb = 1
Works nicely, presenting data in row order, first from both fields (brandname & keywords), then from either field, followed
by OR'ing the lot, which puts the least relevent rows last.
BUT gives duplicate rows, of course.
Here is the the modified query prodkey being the unique identifier
Select * from products where prodkey in
(Select distinct prodkey from products where prodkey in
(Select prodkey from products where
( BrandName Like '%fish%' AND BrandName Like '%oil%')
and
( Keywords Like '%fish%' AND Keywords Like '%oil%')
AND DisplayOnWeb = 1
union all
Select prodkey from products where
( BrandName Like '%fish%' AND BrandName Like '%oil%')
or ( Keywords Like '%fish%' AND Keywords Like '%oil%')
AND DisplayOnWeb = 1
union all
Select prodkey from products where
( BrandName Like '%fish%' or BrandName Like '%oil%')
or ( Keywords Like '%fish%' or Keywords Like '%oil%') AND DisplayOnWeb = 1))
The rows returned do not take into account the order in which the queries are executed.
Anyway of preserving that order?
Thank you
abraxas
suffering from from an ordering dilemma in which using three queries (on same table) using Union all presents data in order in which the queries are executed and yet gives duplicates, presents the data in an ambiguous order format when using it as a sub query.
Here is the basic union query
Select * from products where
( BrandName Like '%fish%' AND BrandName Like '%oil%')
AND
( Keywords Like '%fish%' AND Keywords Like '%oil%')
AND
DisplayOnWeb = 1
union all
Select * from products where
( BrandName Like '%fish%' AND BrandName Like '%oil%')
or
( Keywords Like '%fish%' AND Keywords Like '%oil%')
AND
DisplayOnWeb = 1
union all
Select * from products where
( BrandName Like '%fish%' or BrandName Like '%oil%')
or
( Keywords Like '%fish%' or Keywords Like '%oil%')
AND DisplayOnWeb = 1
Works nicely, presenting data in row order, first from both fields (brandname & keywords), then from either field, followed
by OR'ing the lot, which puts the least relevent rows last.
BUT gives duplicate rows, of course.
Here is the the modified query prodkey being the unique identifier
Select * from products where prodkey in
(Select distinct prodkey from products where prodkey in
(Select prodkey from products where
( BrandName Like '%fish%' AND BrandName Like '%oil%')
and
( Keywords Like '%fish%' AND Keywords Like '%oil%')
AND DisplayOnWeb = 1
union all
Select prodkey from products where
( BrandName Like '%fish%' AND BrandName Like '%oil%')
or ( Keywords Like '%fish%' AND Keywords Like '%oil%')
AND DisplayOnWeb = 1
union all
Select prodkey from products where
( BrandName Like '%fish%' or BrandName Like '%oil%')
or ( Keywords Like '%fish%' or Keywords Like '%oil%') AND DisplayOnWeb = 1))
The rows returned do not take into account the order in which the queries are executed.
Anyway of preserving that order?
Thank you
abraxas