我正在尝试将一些数据从旧表转换为新结构,在该结构中,我需要将单个键ID转换为复合键,这给我带来了麻烦:
CREATE TABLE `tmplt_spoergsmaal` (
`SpID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`lbnr` int(10) unsigned DEFAULT NULL,
`SpTekst` text,
`SpTitel` varchar(100) NOT NULL DEFAULT '',
`fk_Naeste_Sp` int(10) unsigned DEFAULT NULL,
`kontrol` tinyint(3) unsigned NOT NULL DEFAULT 0,
`kontrol_kilde` int(10) unsigned NOT NULL DEFAULT 0,
FOREIGN KEY (kontrol_kilde)
REFERENCES tmplt_spoergsmaal(SpID)
ON DELETE ignore
)
请注意,SPID是顺序的
+--------+--------+-----------+-----------+------------+-----------------+
| SpID | lbnr | SpTekst | SpTitel | kontrol | kontrol_kilde |
+--------+--------+-----------+-----------+------------+-----------------+
| 9000 | 100 | blablabla | title1 | 0 | null |
+--------+--------+-----------+-----------+------------+-----------------+
| 9001 | 101 | blablabla | title2 | 0 | null |
+--------+--------+-----------+-----------+------------+-----------------+
| 9002 | 102 | blablabla | title3 | 0 | null |
+--------+--------+-----------+-----------+------------+-----------------+
| 9003 | 103 | blablabla | title4 | 1 | 9000 |
+--------+--------+-----------+-----------+------------+-----------------+
| 9004 | 104 | blablabla | title5 | 1 | 9001 |
+--------+--------+-----------+-----------+------------+-----------------+
我正在重新设计数据库,并使用lbnr
列而不是kontrol_kilde
列。我的初步查询是这样:
SELECT spid, lbnr, kontrol, kontrol_kilde, (lbnr- (spid - kontrol_kilde)* kontrol)* kontrol AS k
FROM tmplt_spoergsmaal;
这解决了我的问题,但是一个问题(由于减法的翻转(spid - kontrol_kilde
变成了kontrol_kilde - spid
)在某一点出现,这使得方程的一部分为负。未签名,这会导致错误:
Error Code: 1690. BIGINT UNSIGNED value is out of range in
我可以“投射”别名列k
中的列,以便它是int
而不是unsigned int
吗?
答案 0 :(得分:1)
好吧,您可以将cast()
设置为signed
:
SELECT spid, lbnr, kontrol, kontrol_kilde,
cast(lbnr - (spid - kontrol_kilde) * kontrol)* kontrol as signed) AS k
FROM tmplt_spoergsmaal;