简体   繁体   中英

Join 4 tables and update in 5th table MySql

I am trying to join 4 tables and update 5th table

INSERT INTO relation_table(cid,pid,liid,lnid,lgid,l_key)
SELECT  a.cid,
        a.pid,
        b.liid,
        c.lnid,
        d.lang,
        md5("a.cid a.pid b.liid c.lnid d.lang")
FROM links a
        INNER JOIN links_table b
            ON a.lurl = b.lurl
        INNER JOIN lname_table c
            ON a.lname = c.lname
        INNER JOIN lang_table d
            ON a.lang = d.lang
where a.lurl = "google"

Master table links has all the values in it and other 3 tables are sub tables with there own id + value which i am using to join then and update there id in relation table.

EDIT

sorry i did not mention error its md5 [Err] 1062 - Duplicate entry '78527d845cc56e5d28e019d22565f2d7' for key 'l_key' when i am trying to add 1 record its ok but with 2nd record its not working, b.liid is different in 2nd one ... but still its md5 is same as 1st one . its not working, i am not sure if this is right way to do it.

Please help

You are currently always passing the same string to the md5() function. For the first record you INSERT there is no problem, but as soon as you try inserting a second record you get the duplicate entry error.

md5("a.cid a.pid b.liid c.lnid d.lang")

I believe you instead intended to build a unique string based on the 5 identifiers. You could try something along these lines instead:

md5(CONCAT(a.cid, a.pid, b.liid, c.lnid, d.lang))

The problem is with the md5() function:

md5("a.cid a.pid b.liid c.lnid d.lang")

This way the md5 hash of the fixed "a.cid a.pid b.liid c.lnid d.lang" string will be generated. Use

md5(concat(a.cid, a.pid, b.liid, c.lnid, d.lang))

instead. Also, you should consider if your query could return the same set of ids multiple times. If yes, then use the distinct keyword in the select.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM