您好我正在使用PostegreSQL,我想约束一定数量(税号)以遵守以下规则:
x = 8th * 2 + 7th * 3 + 6th * 4 + 5th * 5 + 4th * 6 + 3rd * 7 + 2nd * 8 + 1st + 9
y = x % 11
if (y == 0 || x == 1) z = 0;
else z = 11 - y
这在sql中是否可行?
答案 0 :(得分:0)
是的,虽然很麻烦但有可能。它看起来像这样:
check ((col regexp_matches '^[0-9]{9}$) and
(case when (substring(col, 1, 1) * 9 + substring(col, 2, 1) * 8 + substring(col, 3, 1) * 7 +
substring(col, 4, 1) * 6 + substring(col, 5, 1) * 5 + substring(col, 6, 1) * 4 +
substring(col, 7, 1) * 3 + substring(col, 8, 1) * 2
) % 11 = 0 or
(substring(col, 1, 1) * 9 + substring(col, 2, 1) * 8 + substring(col, 3, 1) * 7 +
substring(col, 4, 1) * 6 + substring(col, 5, 1) * 5 + substring(col, 6, 1) * 4 +
substring(col, 7, 1) * 3 + substring(col, 8, 1) * 2
) = 1
then 0
else 11 -
((substring(col, 1, 1) * 9 + substring(col, 2, 1) * 8 + substring(col, 3, 1) * 7 +
substring(col, 4, 1) * 6 + substring(col, 5, 1) * 5 + substring(col, 6, 1) * 4 +
substring(col, 7, 1) * 3 + substring(col, 8, 1) * 2
) % 11
)
end) = substring(col, 9, 1) + 0
)
就个人而言,我会将其封装在一个函数中并使用该函数。