【问题标题】:Complex Update Query with Nested Select Android SQLite使用嵌套选择 Android SQLite 的复杂更新查询
【发布时间】:2012-06-10 09:31:43
【问题描述】:

android noob... 我有两个表,country_tbl 和 city_tbl 之间存在一对多关系,我想将 city_tbl.landmark_col 中的值与 GROUP_CONCAT() 和 INSERT 所有landmark_col 值作为单个字符串放入 country_tbl.all_landmarks 列。 SQL 似乎需要一个嵌套的 SELECT 来连接 landmark_col 值,然后再将它们传递给 country_tbl ......类似于:

UPDATE country_tbl 
SET country_tbl.all_landmarks = (SELECT landmarks_col FROM 
                                    (SELECT country_id, group_concat(landmarks_col) 
                                            FROM city_tbl INNER JOIN country_tbl
                                            ON country_tbl.country_id = city_tbl.country_id
                                            GROUP BY country_tbl.country_id) 
                                    AS country_landmarks      
                                WHERE country_tbl.country_id = country_landmarks.country_id)
WHERE
EXISTS (
    SELECT *
    FROM country_landmarks
    WHERE country_tbl.country_id = country_landmarks.country_id
);

不确定是否甚至支持嵌套选择语句,或者是否过于占用资源......必须有更好的方法,因为使用 rawQuery 似乎不是最好的解决方案。不确定我是否应该创建临时表、使用 ContentProviders 或传递游标...?

【问题讨论】:

    标签: android sqlite select


    【解决方案1】:

    我通过将长 SQL 查询分成两部分来回答这个问题。首先,我使用 SQLiteQueryBuilder 创建了一个子查询,并使用 rawQuery 运行以获取具有 location_id 和地标名称的 group_concat 值的两列游标。然后,我能够循环浏览光标以使用该国家所有地标名称的每个适当的连接值来更新国家表。

    下面的查询比上面的问题稍微复杂一点(我在发布之前对其进行了简化),只是因为我必须通过 landmark_type_id 将一个 landmark_type 表与另一个 landmark_type 表连接起来,而我的真正目标是连接较短的国家/地区的地标类型列表,而不是国家/地区所有地标名称的长列表。无论如何,它有效。

        public void UpdateCountryLandmarks() throws SQLException {
        Cursor c = null;
        String subquery = SQLiteQueryBuilder.buildQueryString(
                // include distinct
                true,
                // FROM tables
                LANDMARK_TYPE_TABLE + "," + LANDMARKS_TABLE,
                // two columns (one of which is a group_concat()
                new String[] { LANDMARKS_TABLE + "." + LOCATION_ID + ", group_concat(" + LANDMARK_TYPE_TABLE + "." + LANDMARK_TYPE + ",\", \") AS " + LANDMARK_NAMES },
                // where
                LANDMARK_TYPE_TABLE + "." + LANDMARK_ID + "=" + LANDMARKS_TABLE + "." + LANDMARK_TYPE_ID,
                // group by
                LANDMARKS_TABLE + "." + LOCATION_ID, null, null, null);
    
        c = mDb.rawQuery(subquery, null);
    
        if (c.moveToFirst()) {
            do {
                String locationId = c.getString(c.getColumnIndex(LOCATION_ID));
                String landmarkNames = c.getString(c.getColumnIndex(LANDMARK_NAMES));
                ContentValues cv = new ContentValues();
                cv.put(LANDMARK_NAMES, landmarkNames);
                mDb.update(COUNTRY_TABLE, cv, LOCATION_ID + "=" + locationId, null);
            } while (c.moveToNext());
        }
    
        c.close();
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2019-09-25
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2021-05-02
      相关资源
      最近更新 更多