word
word

Reputation: 25

Select from multiple rows

table ad:

id uid name                 description     
1  1   BMW Z5               good condition
2  1   Sell house           new           

table ad_values

id sid    m_input   m_value
1  1      car       BMW
2  1      year      2010
3  1      category  1
4  2      year      2005

How can I search in table 'ad_values'? For example I need car BMW which build year is 2010. I tried something like this:

SELECT * FROM ad_values
WHERE (m_value='BMW' AND m_input='car') AND (m_input='year' AND m_value='2010') AND (m_input='category' AND m_value='1')
GROUP BY sid

Then I get SID and SELECT from table 'ad'. But it doesn't work.

Upvotes: 2

Views: 204

Answers (1)

karlgrz
karlgrz

Reputation: 14751

It seems like you want the results to come from table 'ad' after providing search query terms for table 'ad_values'. Is this something like what you want?

UPDATE

I fixed the query to correctly return matching records.

SELECT
    DISTINCT a.*
FROM
    ad_values v,
    ad a
WHERE
    v.sid = a.id AND
    (  
        (v.m_value='BMW' AND v.m_input='car') OR
        (v.m_input='year' AND v.m_value='2010') OR
        (v.m_input='category' AND v.m_value='1')
    )

This query should yield the following results:

id uid name description
1 1 BMW Z5 good condition

Sound about right?

Upvotes: 2

Related Questions