I know, this question was asked many times and answered as well..
But, my problem is more specific and i couldn't found a proper solution.
我知道,这个问题被多次询问并回答了......但是,我的问题更具体,我找不到合适的解决方案。
My table (named as pages) is like that;
我的表(命名为页面)就是这样;
id (int)
title (text)
content (text)
slug (text)
I need to update my record if slug (i've converted it unique) is same as posted.
如果slug(我已将其转换为唯一的)与发布相同,我需要更新我的记录。
I mean i need to update/insert my record based on slug record.
我的意思是我需要根据slug记录更新/插入我的记录。
Eg. current data:
例如。当前数据:
id | title | content | slug |
--------------------------------------
1 | MainPage| some html | mainpage
if posted data has title=ChildPage, content=html.., slug=mainpage
then i need to update previous record (title and content records), but if data has title=MainPage, content=html.., slug=other_slug
then i need to insert this data as new with a new id.
如果发布的数据有title = ChildPage,content = html ..,slug = mainpage那么我需要更新以前的记录(标题和内容记录),但如果数据有title = MainPage,content = html ..,slug = other_slug那么我需要将此数据作为新数据插入新ID。
UPDATE
UPDATE
Slug record was converted to UNIQUE key.
Slug记录被转换为UNIQUE键。
1 个解决方案
#1
3
The most appropriate thing I can imagine is to create a unique index on the slug column
我能想象的最合适的事情是在slug列上创建一个唯一的索引
ALTER TABLE pages ADD UNIQUE KEY slug;
The reason is simple: if this is not unique-constrained, there may be more than one slug with "mainpage"... which one should be updated??
原因很简单:如果这不是唯一约束的,那么可能会有多个带有“主页”的slug ...哪一个应该更新?
then use ON DUPLICATE KEY UPDATE clause:
然后使用ON DUPLICATE KEY UPDATE子句:
INSERT INTO pages
VALUES (NULL, $title, $content, $slug) ON DUPLICATE KEY UPDATE content=$content
#1
3
The most appropriate thing I can imagine is to create a unique index on the slug column
我能想象的最合适的事情是在slug列上创建一个唯一的索引
ALTER TABLE pages ADD UNIQUE KEY slug;
The reason is simple: if this is not unique-constrained, there may be more than one slug with "mainpage"... which one should be updated??
原因很简单:如果这不是唯一约束的,那么可能会有多个带有“主页”的slug ...哪一个应该更新?
then use ON DUPLICATE KEY UPDATE clause:
然后使用ON DUPLICATE KEY UPDATE子句:
INSERT INTO pages
VALUES (NULL, $title, $content, $slug) ON DUPLICATE KEY UPDATE content=$content