问题
I have the following query that works when the Lookup table has something in it. However if "z.a" doesn't equal anything the INSERT fails. Why would this be?
INSERT INTO dataTable(
a,
b,
c,
d,
e,
f,
g,
h,
i,
j,
k,
l,
m,
n,
o,
p
)SELECT
'000003',
COALESCE(z.Registration, 'REG6'),
'PFTEST',
'1',
'1',
'37000.0',
'148.0',
'439.8',
'1312475688',
'0',
'54',
COALESCE(z.TypeCode, 'A555'),
'',
'1173',
'0',
'nJ'
FROM
LookupTable z
WHERE
z.a = '000003'
回答1:
The outer insert query will insert anything that the select returns - if it returns nothing, then what is there to insert? This is expected behavior. Anything else would be insane - what should get inserted if there's nothing available to be inserted? Should the insert query make up data?
回答2:
In the end I removed the WHERE completely and performed selects within the VALUES direct. Works very well
来源:https://stackoverflow.com/questions/6945332/mysql-insert-with-lookup-when-null