我在 SQLite 中創建查詢時遇到問題。
我有兩個通過 ID 關聯的 SQL 表,我想在加入它們后轉置一些行。
由于我無法共享來自真實資料庫的資訊,因此我創建了一個玩具資料庫來更好地說明我想要實作的目標。在這里,我有一個MAINTABLE,包含它自己的 id 和更多的東西,還有一個SECONDARYTABLE,包含它自己的 id、對 的參考MAINTABLE和鍵/值對。
MAINTABLE
idMainTable MoreStuff
1 asdf
2 fdsa
3 hjkl
4 lkhj
SECONDARY TABLE
idSecondaryTable idMainTable key value
1 1 Key1 a
2 1 Key5 s
3 1 Key7 d
4 1 Key8 f
5 2 Key1 g
6 2 Key4 h
7 2 Key25 j
8 3 Key2 l
9 3 Key6 z
10 4 Key7 y
我想在這里做的是一個能夠連接這兩個表的查詢,并將鍵和值行轉置為這樣的列,因此鍵是結果表中的列:
EXPECTED TABLE
idMainTable MoreStuff Key1 Key2 Key4 Key5 Key6 Key7 Key8 Key25
1 asdf a null null s null d f null
2 fdsa g null h null null null null j
3 hjkl null l null null z null null null
4 lkhj null null null null null y null null
我不介意鍵是否已排序,或者空單元格是否顯示為空或空單元格。
我從這個鏈接知道,當不同鍵的名稱已知時,可以在此處應用條件聚合。但是,我無法知道鍵的數量或鍵的可能名稱,這就是我尋找動態解決方案的原因。在此鏈接中還提出了一個名為pivot_vtab的 SQLite 擴展,但擴展的使用在我的專案中是一個限制,我無法使用它。
在 MySQL 中,可以選擇為此使用GROUP_CONCAT。我在 MySQL 中嘗試過,它有效。但是,我一直在 SQLite 中嘗試類似的方法,但我無法完成這項作業。
這是在 MySQL 中作業的查詢,給出了所需的結果:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(IF(keyColumn = ''',
keyColumn,
''', value, NULL)) AS ',
keyColumn
)
) INTO @sql
FROM (MainTable INNER JOIN SecondaryTable ON MainTable.idMainTable =
SecondaryTable.idMainTable);
SET @sql = CONCAT("SELECT SecondaryTable.idMainTable, ", @sql,
" FROM (MainTable INNER JOIN SecondaryTable ON MainTable.idMainTable =
SecondaryTable.idMainTable)
GROUP BY SecondaryTable.idMainTable");
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
這是在 SQLite 中創建玩具資料庫的代碼:
PRAGMA foreign_keys = ON;
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS `MainTable` (
`idMainTable` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`MoreStuff` VARCHAR(45) NOT NULL,
UNIQUE (`idMainTable` ASC));
CREATE TABLE IF NOT EXISTS `SecondaryTable` (
`idSecondaryTable` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`idMainTable` INTEGER NOT NULL,
`keyColumn` VARCHAR(45) NOT NULL,
`value` VARCHAR(45) NOT NULL,
UNIQUE (`idSecondaryTable` ASC),
CONSTRAINT `fk_SecondaryTable_1`
FOREIGN KEY (`idMainTable`)
REFERENCES `MainTable` (`idMainTable`)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
COMMIT;
BEGIN TRANSACTION;
INSERT INTO `MainTable` (`MoreStuff`) VALUES ('asdf');
INSERT INTO `MainTable` (`MoreStuff`) VALUES ('fdsa');
INSERT INTO `MainTable` (`MoreStuff`) VALUES ('hjkl');
INSERT INTO `MainTable` (`MoreStuff`) VALUES ('lkhj');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (1, 'Key1', 'a');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (1, 'Key5', 's');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (1, 'Key7', 'd');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (1, 'Key8', 'f');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (2, 'Key1', 'g');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (2, 'Key4', 'h');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (2, 'Key25', 'j');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (3, 'Key2', 'l');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (3, 'Key6', 'z');
INSERT INTO `SecondaryTable` (`idMainTable`, `keyColumn`, `value`) VALUES (4, 'Key7', 'y');
COMMIT;
出于測驗目的,我使用以下在線 SQLite IDE來生成所需的查詢。
有沒有辦法在沒有擴展的情況下使用 SQLite 實作我所描述的?
uj5u.com熱心網友回復:
看起來沒有辦法使用純 SQLite 來實作這一點,正如評論中所示。
正如評論中所建議的那樣,可以使用另一種編程語言(例如 C )構建動態查詢。
以下鏈接也證實了這一點:
StackExchange:是??否可以純粹在 SQLite 中創建動態 SQL 陳述句?
StackExchange:如何在 SQLite 中透視資料
StackOverflow:SQLITE 將大量行轉換為列
如果這在將來發生變化,我會接受其他答案,或者如果我自己發現則更新這個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/365109.html
