MySQL INSERT with lookup when null

烂漫一生 提交于 2020-02-25 13:31:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!