Search code examples
mysqlsqljoinsql-like

Updating a Table Column from a Count in another table


I'm trying to update a column in a table with a result of COUNT from another table. Here is the code: (And it's working)

UPDATE software2
SET totalsoft = (
   SELECT COUNT(*)
   FROM links
   WHERE sftwr = software2.softwarename)

But when I try to use the LIKE instance instead the = (in last line) I get no results... this is the code:

UPDATE software2
SET totalsoft = (
   SELECT COUNT(*)
   FROM links
   WHERE sftwr LIKE '%software2.softwarename%')

Occurrences are 0. Any help?


Solution

  • UPDATE software2 
    SET totalsoft = ( SELECT COUNT(*) 
                      FROM links 
                      WHERE sftwr LIKE concat('%', software2.softwarename, '%')
                    )
    

    If you put the column name in quotes then it will be treated as string. Then not the column content will be used but the static string 'software2.softwarename'