CREATE TABLE rootTable( id INTEGER PRIMARY KEY AUTOINCREMENT, idName VARCHAR(10) NOT NULL);CREATE TABLE dataTable( id INTEGER PRIMARY KEY AUTOINCREMENT, rootTableId INTEGER(10) NOT NULL, rootTableIdName VARCHAR(10));
SQLUPDATE dataTable SET rootTableIdName = (SELECT idName FROM rootTable WHERE rootTable.id = rootTableId);
SQLCREATE TABLE dataTable( id INTEGER PRIMARY KEY AUTOINCREMENT, flagId INTEGER, squenceId INTEGER, );
C++ (Qt)QSqlDatabase db;int colId; ...QSqlQuery query(db);if (query.exec(QString("CREATE TEMP TABLE tempSequence(id INTEGER PRIMARY KEY AUTOINCREMENT, dataTableId INTEGER NOT NULL)"))) { if (query.exec(QString("INSERT INTO tempSequence(dataTableId) SELECT id FROM dataTable WHERE flagId = %1)") .arg( QVariant( colId ).toString()) )) { if (query.exec(QString("UPDATE dataTable SET sequenceId = (SELECT id FROM tempSequence WHERE dataTable.id = dataTableId"))) { //dosmth } }}
SQLINSERT INTO tempSequence(dataTableId) SELECT id FROM dataTable WHERE flagId = %1 ORDER BY id)
SQLsqlite> CREATE TABLE t (id integer PRIMARY KEY, flagId integer, sequence integer);sqlite> INSERT INTO t (flagId) VALUES (1);sqlite> INSERT INTO t (flagId) VALUES (1);sqlite> INSERT INTO t (flagId) VALUES (1);sqlite> INSERT INTO t (flagId) VALUES (2);sqlite> INSERT INTO t (flagId) VALUES (2);sqlite> INSERT INTO t (flagId) VALUES (2);sqlite> INSERT INTO t (flagId) VALUES (2);sqlite> SELECT * FROM t;1|1|2|1|3|1|4|2|5|2|6|2|7|2|sqlite> CREATE temp TABLE tt(id integer PRIMARY KEY, t_id integer);sqlite> INSERT INTO tt(t_id) SELECT id FROM t WHERE flagId = '1';sqlite> SELECT * FROM tt;1|12|23|3sqlite> UPDATE t SET sequence = (SELECT id FROM tt WHERE t.id = t_id ORDER BY id);sqlite> DELETE FROM tt;sqlite> SELECT * FROM t;1|1|12|1|23|1|34|2|5|2|6|2|7|2|sqlite> SELECT * FROM tt;sqlite> INSERT INTO tt(t_id) SELECT id FROM t WHERE flagId = '2';sqlite> SELECT * FROM tt;1|42|53|64|7sqlite> UPDATE t SET sequence = (SELECT id FROM tt WHERE t.id = t_id ORDER BY id);sqlite> SELECT * FROM t;1|1|2|1|3|1|4|2|15|2|26|2|37|2|4sqlite>
SQLUPDATE t SET sequence = (SELECT id FROM tt WHERE t.id = t_id ORDER BY id);
SQLUPDATE t SET sequence = (SELECT id FROM tt WHERE t.id = t_id ORDER BY id) WHERE sequence IS NULL;