简体   繁体   English

加入4个表并在第5个表中更新MySql

[英]Join 4 tables and update in 5th table MySql

I am trying to join 4 tables and update 5th table 我正在尝试加入4个表并更新第5个表

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. 主表链接具有所有值,其他3个表是具有自己的ID +值的子表,我将使用这些ID +值进行连接,然后在关系表中更新那里的ID。

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 . 抱歉,我没有提到错误,它的md5 [Err] 1062-当我尝试添加1条记录的确定但第二条记录不起作用时,键'l_key'的条目'78527d845cc56e5d28e019d22565f2d7'重复了,b.liid在第二个中是不同的。 ..但它的md5仍然与第一个相同。 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. 当前,您始终将相同的字符串传递给md5()函数。 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. 对于您INSERT的第一条记录没有问题,但是一旦您尝试插入第二条记录,就会出现重复输入错误。

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. 我相信您反而打算基于5个标识符构建一个唯一的字符串。 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()函数上:

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. 这样,将生成固定的“ a.cid a.pid b.liid c.lnid d.lang”字符串的md5哈希。 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. 另外,您应该考虑查询是否可以多次返回同一组ID。 If yes, then use the distinct keyword in the select. 如果是,则在选择中使用distinct关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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