MySQL INSERT with lookup when null

2019-08-05 08:45发布

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' 

标签: mysql insert
2条回答
冷血范
2楼-- · 2019-08-05 09:36

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?

查看更多
别忘想泡老子
3楼-- · 2019-08-05 09:43

In the end I removed the WHERE completely and performed selects within the VALUES direct. Works very well

查看更多
登录 后发表回答