1

一旦将项目添加到表中,我需要使用路径更新列“路径”。我为此使用了 mySql 和 PHP。

"id"    "name"         "description"               "level"  "parent"    "country"   "path"
"1"     "Kitchenware"   "Kitchenware description"   "1"       "0"         "US"        "Kitchenware"
"2"     "Knives"        "All our knives"            "2"       "1"         "US"        "Kitchenware > Knives"
"3"     "Butter Knives" "All Butter Knives"         "3"       "2"         "US"        "Kitchenware > Knives > Butter Knives"
"4"     "Cut em all"    "Cut em all"                "4"       "3"         "US"        "Kitchenware > Knives > Butter Knives > Cut em all"
"5"     "Cull em all"   "Cull em all"               "4"       "3"         "US"        
"6"     "Smear em all"  "Smear em all"              "4"       "3"         "US"        
"7"     "Meat Knives"   "All Meat Knives"           "3"       "2"         "US"        
"8"     "Cut em meat"   "Cut em meat"               "4"       "7"         "US"        
"9"     "Cull em meat"  "Cull em meat"              "4"       "7"         "US"        
"10"    "Smear em meat" "Smear em meat"             "4"       "7"         "US"        

关于这样做的好方法有什么想法吗?

在此表中,级别 1 是最高的。2 和 3 正在前进。4是物品。

我有点坚持这样做(触发器内的选择)。它需要在每次插入后运行。不是一下子。

4

2 回答 2

1

您需要一个存储过程而不是触发器,因为它有一个限制:

在存储的函数或触发器中,不允许修改调用函数或触发器的语句已经使用(用于读取或写入)的表。

但使用存储过程,您可以:

尽管某些限制通常适用于存储函数和触发器,但不适用于存储过程,但如果从存储函数或触发器中调用存储过程,则这些限制确实适用于存储过程。

文档

像这样的东西:

CREATE PROCEDURE `build_path`(IN row_id INT, IN path_name VARCHAR(255))
BEGIN

UPDATE `my_table` SET `path`=path_name WHERE `id`=row_id;

END$$

一个示例调用是CALL build_path(1,'Kitchenware');

假设您已经构建了路径,如果您没有构建路径,则可以更改该过程以构建路径。

更新:

构造路径的查询。注意:自连接数 = MAX(level)

SELECT
    CONCAT(
        m1.path,
        IF(m2.path IS NULL,'',CONCAT(' > ',m2.path)),
        IF(m3.path IS NULL,'',CONCAT(' > ',m3.path)),
        IF(m4.path IS NULL,'',CONCAT(' > ',m4.path))
    ) as za_path
FROM 
    my_table as m1
LEFT JOIN my_table as m2
    ON m2.parent = m1.id
LEFT JOIN my_table as m3
    ON m3.parent = m2.id
LEFT JOIN my_table as m4
    ON m4.parent = m3.id
WHERE 
    m1.id = 8
于 2013-05-07T11:55:23.720 回答
0

请试试这个跳这个样本将解决你的问题

CREATE TRIGGER dbo.trgforyou ON YourTable
FOR INSERT
AS
DECLARE @ID INT
SELECT @ID=inserted.iID From inserted


update Table set "name = '',description='',level='',parent='',country=''path='' Where iid = @ID
于 2013-05-07T11:49:04.383 回答