Search code examples
mysqlsqlinner-join

Error in joining two table using INNER JOIN with the replace keyword


I am trying to join 2 tables with INNER JOIN but the catch is I want to use REPLACE keyword as in another table there is an extra character in the text. I have written the query as :

SELECT
    keeper_details.*,
    keepers.*
FROM
    keeper_details
    INNER JOIN keepers ON keeper_details.cphh = keepers.CPHH SET keepers.CPHH = REPLACE(keepers.CPHH,"/","")

But I am getting an error saying as :

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET keepers.CPHH = REPLACE(keepers.CPHH,"/","")

My common text is on keeper table(CPHH) = "XXXXXXXX" and on keeper_details table(CPHH) = XX/XXX/XXXX

I hope I am clear. I don't know where I am doing wrong! Any suggestions will be a great help! Thanks in advance!


Solution

  • This should be enough:

    SELECT keeper_details.*
           , keepers.*
    FROM keeper_details
    INNER JOIN keepers
    on keeper_details.cphh = REPLACE(keepers.CPHH,"/","");
    

    When you are using the on word you are telling the query what to use as a "condition" when joining two tables. So when you remove the character '/' when joining all should be ok.

    I also redommend to you to use single quotes when you using string values:

    SELECT keeper_details.*
           , keepers.*
    FROM keeper_details
    INNER JOIN keepers
    on keeper_details.CPHH = REPLACE(keepers.CPHH,'/','')