MySQL returning an empty field: CONCAT(nonEmpty1,empty2,nonEmpty3) = NULL

前端 未结 6 1137
攒了一身酷
攒了一身酷 2021-02-19 10:27

I have PHP 5 code accessing a MyISAM table on MySQL 5 server. The query looks like this:

SELECT CONCAT(fName1,\' \',mName2,\' \',lName3) AS userName 
    FROM us         


        
6条回答
  •  眼角桃花
    2021-02-19 11:19

    This is an answer based on the solution above by @chocojosh and another question here: MySQL/SQL: Update with correlated subquery from the updated table itself

    I had a similar problem, but I was trying to concat a bunch of group_concats and some were NULL which caused the whole row to be NULL. The goal was to place data from other tables into a single field in the main table to allow fulltext searches.

    Here is the SQL that worked. Note the first parameter for concat_ws I made ' ', this allowed for spaces between the values for the fulltext field.

    Hope this helps someone.

    update
    products target
    INNER JOIN 
    (
        select p.id, 
        CONCAT_WS(
        ' ',
            (select GROUP_CONCAT(field SEPARATOR ' ') from table1 where productId = p.id),
            p.title,' ', 
            (select GROUP_CONCAT(field, ' ', descriptions SEPARATOR ' ') from table2 where productId = p.id),
            (select GROUP_CONCAT(field SEPARATOR ' ') from table3 where productId = p.id),
            (select GROUP_CONCAT(field, ' ', catno SEPARATOR ' ') from table4 where productId = p.id),
            (select GROUP_CONCAT(field SEPARATOR ' ') from table5 where productId = p.id),
            (select GROUP_CONCAT(field SEPARATOR ' ') from table6 where productId = p.id),
            (select GROUP_CONCAT(field SEPARATOR ' ') from table7 where productId = p.id)
        ) as ft
        from products p
    ) as source
    on target.id = source.id
    set target.fulltextsearch = source.ft
    

提交回复
热议问题