MySQL选择两列多个名称值对

时间:2014-05-14 12:02:15

标签: mysql

我需要有关为多列生成查询的帮助。 我的tbl_advert_specific_fields_values表的一部分看起来像:

id advert_id   field_name      field_value
1  654         t1_sqft         50
2  655         t1_yearbuilt    1999
3  1521        t2_doorcount    5
4  656         t1_yearbuilt    2001
5  656         t1_sqft         29
6  654         t1_yearbuilt    2004


SELECT p.*, p.id AS id, p.title AS title, usr.id as advert_user_id,
            p.street_num, p.street,c.icon AS cat_icon,c.title AS cat_title,c.title AS cat_title,
            p.description as description,
            countries.title as country_name,
            states.title as state_name,
            date_FORMAT(p.created, '%Y-%m-%d') as fcreated 
            FROM tbl AS p
                LEFT JOIN tbl_advertmid AS pm ON pm.advert_id = p.id
                INNER JOIN tbl_usermid AS am ON am.advert_id = p.id
                LEFT JOIN tbl_users AS usr ON usr.id = am.user_id
                INNER JOIN tbl_categories AS c ON c.id = pm.cat_id 
                INNER JOIN tbl_advert_specific_fields_values AS asfv ON asfv.advert_id = p.id
                LEFT JOIN tbl_countries AS countries ON countries.id = p.country
                LEFT JOIN tbl_states AS states ON states.id = p.locstate
            WHERE p.published = 1 AND p.approved = 1 AND c.published = 1
                AND (asfv.field_name = 't1_yearbuilt' 
                AND CONVERT(asfv.field_value,SIGNED) <= 2004 )
                AND (asfv.field_name = 't1_sqft' 
                AND CONVERT(asfv.field_value,SIGNED) <= 50)
                AND p.price <= 10174945 AND (p.advert_type_id = 1) 
                AND (c.id = 43 OR c.parent = 43) 
    GROUP BY p.id 
        ORDER BY p.price DESC

好的,问题出在这个动态生成的asfv查询部分。它属于通过其特定字段表示广告的对象。 asfv实际上是advert_specific_fields_values表(表名全部说明)。

没有部分:

AND (asfv.field_name = 't1_yearbuilt'
AND CONVERT(asfv.field_value,SIGNED) <= 2004 )
AND (asfv.field_name = 't1_sqft' 
AND CONVERT(asfv.field_value,SIGNED) <= 50)

查询返回属于advert_type_id的所有广告,其价格低于10.174.945,00€。

我需要的只是查询更新,只返回广告,例如t1_yearbuilt小于2005,t1_sqft小于51(advert_id =&gt; 654,656)。

我还需要查询例如t1_sqft&gt; = 30 AND t1_sqft&lt; = 50(advert_id =&gt; 654)之间的值。

任何人都可以知道如何更新此查询?

TNX

0 个答案:

没有答案