1

我是数据库新手,所以可能很容易。我有以下数据库结构: 表产品

表格过滤器(可能包含类似的东西)

  • 颜色 蓝色
  • 颜色:黑色
  • 红色
  • 尺寸小
  • 品牌 X

表关系(只有产品 ID 和过滤器 ID)

假设我想获取所有黑色或红色且很小的产品,我编写了以下查询:

SELECT products.name FROM products 
JOIN pfrelation ON
    ((pfrelation.filter_id=18) AND (pfrelation.filter_id=11 OR pfrelation.filter_id=13) AND products.id=pfrelation.product_id) 

在上面的例子中,11 和 13 代表黑色和红色,而 18 是小号的 id。您可能会怀疑上述查询没有结果(id 不能同时为 18 和 11/13)。如何编写选择以动态添加任何过滤器组合?如何重写上面示例中的查询?

谢谢

4

2 回答 2

1

我以这种方式动态构建查询:

public ArrayList<Products> getFilteredOnlinePictures( Hashtable<String, List<String>> filters,..)
{
    SQLiteDatabase db = getWritableDatabase();
    // create query conditions
    StringBuffer filtersQuery = new StringBuffer();
    for (String key: Collections.list(filters.keys()))
    {
        List<String> filtersValues = filters.get(key);
        if (filtersQuery.length() > 0)
            filtersQuery.append(" INTERSECT ");
        filtersQuery.append("SELECT " + TABLE_PFRELATIONS + "." + COLUMN_PRODUCT_ID
            + " FROM " + TABLE_PFRELATIONS + " WHERE " + TABLE_PFRELATIONS + "."
            + COLUMN_FILTER_ID + " IN (");
        for (String value: filtersValues)
        {
            long filterID = getIDForFilter(db, key, value);
            filtersQuery.append(filterID);
            if (filtersValues.indexOf(value) == (filtersValues.size() - 1))
            {
                // this is the last value
                filtersQuery.append(")");
            }
            else
            {
                // there are more values
                filtersQuery.append(",");
            }

        }
    }
    if (filtersQuery.length() > 0)
    {
        filtersQuery.append(")");
        filtersQuery.insert(0, " WHERE " + TABLE_PRODUCTS + ".id" + " IN (");
    }
    String sql = "SELECT " + TABLE_PRODUCTS + "." + COLUMN_NAME + " FROM " + TABLE_PRODUCTS + filtersQuery.toString();
    Cursor c = db.rawQuery(sql, null); ...

作为示例给出的查询将是:

SELECT products.name FROM products
WHERE products.id IN (
    SELECT pfrelation.product_id FROM pfrelation WHERE pfrelation.filter_id IN (18) 
    INTERSECT 
    SELECT pfrelation.product_id FROM pfrelation WHERE pfrelation.filter_id IN (11,13)
    ) 

这样我就可以拥有任何具有任意数量值的过滤器。

于 2013-06-26T11:55:36.703 回答
1

you can achieve this with Dynamic Query. I don't much know about PostgreSql, but I know SQL Server, so I am giving a example to do same.

Here is SQLFiddel Demo

Create table Product(pid int,name varchar(10),color varchar(10),brand varchar(10),size varchar(10));
insert into product values(1,'ABC','red','X','small');
create table pfrelation(pid int,fid int,relation varchar(100));
insert into pfrelation values(1,10,'Color=''blue''');
insert into pfrelation values(1,11,'Color=''black''');
insert into pfrelation values(1,13,'Color=''red''');
insert into pfrelation values(1,18,'size=''small''');
insert into pfrelation values(1,20,'brand=''X''');

Declare @sql varchar(200)

select @sql = ((select 'pr.' + relation from pfrelation where fid = 18) 
   + ' and (' + 
(select 'pr.' + relation from pfrelation where fid = 11) 
   + ' or '   + 
(select 'pr.' + relation from pfrelation where fid = 13) 
   + ') and pr.pid=pf.pid' ) 
select @sql
print('select * from Product pr,pfrelation pf where '+@sql)
exec('select * from Product pr,pfrelation pf where '+@sql)
于 2013-06-26T11:42:01.890 回答