我有一个赞美诗实例的数据库,因为它们出现在各种赞美诗中。 该表的设置大致如下:
CREATE TABLE `Hymns` (
`HymnID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`HymnbookID` int(11) DEFAULT NULL,
`HyNumber` int(11) DEFAULT NULL,
`HyName` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`HyFirstLine` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`HyFirstLineDif` tinyint(1) NOT NULL DEFAULT '0',
`SongID` int(11) DEFAULT NULL,
`TextID` int(11) DEFAULT NULL,
`TuneID` int(11) DEFAULT NULL,
PRIMARY KEY (`HymnID`),
KEY `HymnbookID` (`HymnbookID`),
KEY `SongID` (`SongID`)
) ENGINE=MyISAM AUTO_INCREMENT=134381 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `Hymns` (`HymnID`, `HymnbookID`, `HyNumber`, `HyName`,
`HyFirstLine`, `HyFirstLineDif`, `SongID`, `TextID`, `TuneID`)
VALUES (642, 1, 26, 'Joseph Smith’s First Prayer',
'Oh, how lovely was the morning', 1, 26, 26, 26);
有时第一行是相同的,但在不同的地方切断 - 有时它们是不同的(字母A和B不在数据库中,我只是把它们放在一起显示相同和不同):< / p>
哦,早上多可爱[A]
哦,多么可爱[A]
哦,早上多可爱啊!辐射光芒[A]
早上多可爱啊! [B]
是否可以仅返回唯一第一行的最长版本,例如:?
哦,早上多可爱啊!辐射光芒[A]
早上多可爱啊! [B]
这是我到目前为止的查询:
SELECT HyFirstLine
FROM Hymns
WHERE TextID = 26 AND HyFirstLine IS NOT NULL
GROUP BY HyFirstLine
编辑:数据作为字典列表返回到Python。根据评论,也许最好用Python过滤事实?我不确定我会怎么做。添加Python标记。
答案 0 :(得分:0)
编辑:这适用于MSSQL,而不是mySQL。我很抱歉。希望下面的参考可以帮助你。
未经测试,但您可能需要使用以下类似的存储过程:
DECLARE @HyFirstLine varchar(255);
DECLARE @StoredHyFirstLine varchar(255);
DECLARE @OutputTable Table(HyFirstLine varchar(255))
DECLARE hy_cursor CURSOR FOR
SELECT HyFirstLine FROM Hymns;
OPEN hy_cursor;
FETCH NEXT FROM hy_cursor
INTO @HyFirstLine;
WHILE @@FETCH_STATUS = 0
BEGIN
IF NOT EXISTS(SELECT * FROM @OutputTable WHERE HyFirstLine like @HyFirstLine+'%')
BEGIN
INSERT INTO @OutputTable(HyFirstLine) VALUES(@HyFirstLine)
END
ELSE
BEGIN
SET @StoredHyFirstLine = (SELECT HyFirstLine FROM @OutputTable WHERE HyFirstLine like @HyFirstLine+'%')
IF LEN(@HyFirstLine) > LEN(@StoredHyFirstLine)
BEGIN
UPDATE @OutputTable SET HyFirstLine = @HyFirstLine WHERE HyFirstLine like @HyFirstLine+'%'
END
END
FETCH NEXT FROM hy_cursor
INTO @HyFirstLine;
END
CLOSE hy_cursor;
DEALLOCATE hy_cursor;
SELECT * FROM @OutputTable