Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Database Design and Application Architecture
 Complex SQL query

Author  Topic 

manc
Starting Member

2 Posts

Posted - 2009-11-12 : 12:08:41
I am trying to create a search form that uses multiple parameters in multiple columns with multiple values. here's what i have:

a table, called FIELDVALS.
two columns I will be searching in, called "fielddef_id" and "value"

I have built one form with 3 parameters. Each parameter is optional. I therefore have 3 queries for the 3 parameters:

1) fielddef_id IN (8,9) AND value LIKE '%$post%'
2) fielddef_id IN (12) AND value >= '$bedrooms'
3) fielddef_id IN (24) AND value >= '$price'

i can pull off one query at a time, but combining them brings 0 results. I can't seem to find the solution here. My full query is as follows:

$query ="SELECT DISTINCT product_id FROM fieldvals WHERE (fielddef_id IN (8,9) AND value LIKE '%$post%') ";

if ($bedrooms!= '0') {
$query .="AND (fielddef_id IN (12) AND value >= '$bedrooms') ";
}

if ($price!= '0') {
$query .="AND (fielddef_id IN (12) AND value >= '$price')";
}


i know here the problem is combining the multiple queries with AND.

is there any solution to this?

Ifor
Aged Yak Warrior

700 Posts

Posted - 2009-11-12 : 12:31:27
This looks like an horrible EAV design.

-- *** Test Data ***
DECLARE @FieldVals TABLE
(
product_id int NOT NULL
,fielddef_id int NOT NULL
,[value] varchar(50) NOT NULL
)
INSERT INTO @FieldVals
SELECT 1, 8, 'Some post junk'
UNION ALL SELECT 1, 9, 'Another post junk'
UNION ALL SELECT 1, 12, '12 bedrooms and 6 bathrooms'
UNION ALL SELECT 1, 24, 'The price is exorbitant'
UNION ALL SELECT 2, 8, 'Some post junk'
UNION ALL SELECT 2, 12, '12 bedrooms and 6 bathrooms'
UNION ALL SELECT 2, 24, 'The price is exorbitant'
UNION ALL SELECT 3, 12, '12 bedrooms and 6 bathrooms'
UNION ALL SELECT 4, 12, '12 bedrooms and 6 bathrooms'
UNION ALL SELECT 4, 24, 'The price is exorbitant'
-- *** End Test Data ***

SELECT product_id
FROM @FieldVals
WHERE (fielddef_id IN (8,9) AND [value] LIKE '%post%')
OR (fielddef_id = 12 AND [value] LIKE '%bedrooms%')
OR (fielddef_id = 24 AND [value] LIKE '%price%')
GROUP BY product_id
HAVING COUNT(*) >= 3

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-11-12 : 12:40:58
Looks like PHP...

I hope it is MS SQL Server and not MySQL...

To combine these conditions I think it is needed to query like this:

select distinct
f1.product_id
FROM fieldvals f1
WHERE (fielddef_id IN (8,9) AND value LIKE '%$post%')
and exists(select * from fieldvals where fielddef_id IN (12) AND value >= '$bedrooms' and product_id=f1.product_id)
and exists(select * from fieldvals where fielddef_id IN (24) AND value >= '$price' and product_id=f1.product_id)


At the end print your $query, then you can have a look at it and maybe you can see what is wrong.



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-12 : 13:24:29
Now would be a good time to Google out "SQL Injection"
Go to Top of Page
   

- Advertisement -