我有一个包含ID
键列的表,使用binary(16)
存储GUID / UUID,其余列为varchar(45)
。
我编写了一个函数,在给定其他列的情况下为ID
运行select,如果找不到ID
,则创建一个新函数。我的问题是这个函数返回的ID
与我的数据不匹配。如果我只是在工作台上运行函数的select部分,它就不会按预期返回任何内容。
这是我的功能:
CREATE DEFINER=`sa`@`192.100.100.%` FUNCTION `SelectReadingHeaders`(testType VARCHAR(45)
, COL1 VARCHAR(45)
, COL2 VARCHAR(45)
, COL3 VARCHAR(45)
, COL4 VARCHAR(45)
, COL5 VARCHAR(45)
, COL6 VARCHAR(45)
, COL7 VARCHAR(45)
, COL8 VARCHAR(45)
, COL9 VARCHAR(45)
, COL10 VARCHAR(45)
, COL11 VARCHAR(45)
, COL12 VARCHAR(45)
, COL13 VARCHAR(45)
, COL14 VARCHAR(45)
, COL15 VARCHAR(45)
, COL16 VARCHAR(45)) RETURNS binary(16)
BEGIN
DECLARE columnID BINARY(16);
SELECT `ID` FROM `ate`.`readings_columns`
WHERE (`TYPE` <=> testType
AND`COL1` <=> COL1
AND `COL2` <=> COL2
AND `COL3` <=> COL3
AND `COL4` <=> COL4
AND `COL5` <=> COL5
AND `COL6` <=> COL6
AND `COL7` <=> COL7
AND `COL8` <=> COL8
AND `COL9` <=> COL9
AND `COL10` <=> COL10
AND `COL11` <=> COL11
AND `COL12` <=> COL12
AND `COL13` <=> COL13
AND `COL14` <=> COL14
AND `COL15` <=> COL15
AND `COL16` <=> COL16)
LIMIT 1
INTO columnID;
IF columnID IS null THEN
SET columnID = GuidToBinary(UUID());
INSERT INTO `ate`.`readings_columns`
VALUES (columnID
, testType
, COL1
, COL2
, COL3
, COL4
, COL5
, COL6
, COL7
, COL8
, COL9
, COL10
, COL11
, COL12
, COL13
, COL14
, COL15
, COL16);
END IF;
RETURN columnID;
END
下一个块是我的测试调用,它返回错误的结果。表中不存在这组列/测试类型。返回的值来自具有匹配TYPE
列的行,以及末尾的一些空列,但不是完全匹配。 ToGuid是一个使二进制(16)guid可读的函数。:
SELECT ToGuid(SelectReadingHeaders('TCXOHunt','TEMPERATURE','ADC Ratio','AH0','AH1','AH2','BH0','BH1','BH2','INTERNALTEMPERATURE',null,null,null,null,null,null,null))
然而,如果我将select函数键入新的查询选项卡,它会按预期返回0结果:
SELECT ToGuid(`ID`) FROM `ate`.`readings_columns`
WHERE `TYPE` <=> 'TCXOHunt'
AND`COL1` <=> 'TEMPERATURE'
AND `COL2` <=> 'ADC Ratio'
AND `COL3` <=> 'AH0'
AND `COL4` <=> 'AH1'
AND `COL5` <=> 'AH2'
AND `COL6` <=> 'BH0'
AND `COL7` <=> 'BH1'
AND `COL8` <=> 'BH2'
AND `COL9` <=> 'INTERNALTEMPERATURE'
AND `COL10` <=> null
AND `COL11` <=> null
AND `COL12` <=> null
AND `COL13` <=> null
AND `COL14` <=> null
AND `COL15` <=> null
AND `COL16` <=> null
LIMIT 1
答案 0 :(得分:0)
MySQL无法确定您是指列名还是参数或变量名。因此,当这些具有相同的名称时,MySQL会在您编写类似的过程/函数时将列与自身进行比较。