在SQLite android中计算所选列值的频率?

时间:2013-10-01 06:40:08

标签: android sqlite

db结构:

public final class ManufacturersColumns implements MyStyleBaseColumns {
public static final String ALIAS           = "alias";
public static final String MANUFACTURER_ID = "manufacturer_id";
public static final String NAME            = "name";

public static final String TABLE_NAME = "manufacturers";

public static final String CREATE_TABLE =
        "CREATE TABLE " + TABLE_NAME + "( "
                + _ID             + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + ALIAS           + " TEXT, "
                + DATE_DELETED    + " DATE, "
                + DATE_CREATED    + " DATE, "
                + DATE_MODIFIED   + " DATE, "
                + MANUFACTURER_ID + " TEXT, "
                + NAME            + " TEXT "
        + ");";}






public final class ManufacturersUserCreatedColumns implements MyStyleBaseColumns {
    public static final String MANUFACTURER_ID = "manufacturer_id";
    public static final String NAME            = "name";

    public static final String TABLE_NAME = "manufacturers_user_created";

    public static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + "( "
                    + _ID             + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + MANUFACTURER_ID + " TEXT, "
                    + NAME            + " TEXT, "
                    + DATE_CREATED    + " DATE, "
                    + DATE_MODIFIED   + " DATE, "
                    + DATE_DELETED    + " DATE, "
                    + SYNC_FLAG       + " NUMERIC " 
            + ");";
}

现在我们使用下面提到的查询来列出名称:

<string name="kGetAllManufacturers"><![CDATA["SELECT _id, manufacturer_id, name, alias, usergen FROM (SELECT _id, manufacturer_id, name, alias, 0 AS usergen FROM manufacturers UNION SELECT _id, manufacturer_id, name, '' AS alias, 1 AS usergen FROM manufacturers_user_created) main ORDER BY lower(name) ASC"]]>

要求是:

需要更改以上经常在顶部选择的列表名称的查询。

先谢谢。

1 个答案:

答案 0 :(得分:0)

最后,我通过使用此查询解决了我的问题。

<string name="kGetAllManufacturers"><![CDATA["SELECT _id, man_id, name, alias, usergen, count(man_id) as amount from (SELECT c._id as _id, c.manufacturer_id as man_id, m.name as name, m.alias as alias, 0 AS usergen FROM clothes c INNER JOIN manufacturers m ON m.manufacturer_id = c.manufacturer_id UNION all SELECT c._id as _id, c.manufacturer_id as man_id, ucm.name as name, '' AS alias, 1 AS usergen FROM clothes c INNER JOIN manufacturers_user_created ucm ON ucm.manufacturer_id = c.manufacturer_id UNION SELECT _id, manufacturer_id, name, alias, 0 AS usergen FROM manufacturers UNION SELECT _id, manufacturer_id, name, '' AS alias, 1 AS usergen FROM manufacturers_user_created ) main GROUP BY man_id, name ORDER BY COUNT(man_id) desc"]]></string>