Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果目标字段为空,MySQL将在字段之间复制字符串_Mysql_Sql_String_Sql Update_Where Clause - Fatal编程技术网

如果目标字段为空,MySQL将在字段之间复制字符串

如果目标字段为空,MySQL将在字段之间复制字符串,mysql,sql,string,sql-update,where-clause,Mysql,Sql,String,Sql Update,Where Clause,我正在使用WordPress数据库,其中一些post\u title字段为空。我需要用字段post\u content中的字符串填充这些内容。字符串位于两个标记之间:文本字符串 我可以使用以下方法成功查找和复制字符串: UPDATE `wp_posts` SET `post_title` = SUBSTRING_INDEX(SUBSTRING_INDEX(`post_content`, '<strong>', -1), '</strong>', 1) FROM `wp_

我正在使用WordPress数据库,其中一些
post\u title
字段为空。我需要用字段
post\u content
中的字符串填充这些内容。字符串位于两个标记之间:
文本字符串

我可以使用以下方法成功查找和复制字符串:

UPDATE `wp_posts` 
SET `post_title` = SUBSTRING_INDEX(SUBSTRING_INDEX(`post_content`, '<strong>', -1), '</strong>', 1)
FROM `wp_posts`
WHERE `post_title` = ''
但我不确定如何将这两种行动结合起来——或者这是否是解决问题的正确方法

其中post_title=''不起作用。这将覆盖所有post_标题字段,而不仅仅是空字段

这根本不是事实。但是,您的查询不是有效的MySQL语法。正确的查询是:

UPDATE `wp_posts` 
    SET `post_title` = SUBSTRING_INDEX(SUBSTRING_INDEX(`post_content`, '<strong>', -1), '</strong>', 1)
    WHERE `post_title` = '';
UPDATE`wp_posts`
设置'post\u title`=子字符串索引(子字符串索引('post\u content`,'',-1),'',1)
其中,`post_title`='';

MySQL不支持
UPDATE
中的
FROM
子句。可能执行的只是查询的前两行。

您的代码不是有效的MySQL语法,应该会出错。MySQL不希望在
update
语句中使用
from
子句,因此应该是:

update `wp_posts` 
set `post_title` = substring_index(substring_index(`post_content`, '<strong>', -1), '</strong>', 1)
where `post_title` = ''

您的代码无效,必须检查空值

UPDATE `wp_posts` 
SET `post_title` = SUBSTRING_INDEX(SUBSTRING_INDEX(`post_content`, '<strong>', -1), '</strong>', 1)
WHERE `post_title` IS  NULL
UPDATE`wp_posts`
设置'post\u title`=子字符串索引(子字符串索引('post\u content`,'',-1),'',1)
其中'post_title'为空
where `post_title` = '' or `post_title` is null
UPDATE `wp_posts` 
SET `post_title` = SUBSTRING_INDEX(SUBSTRING_INDEX(`post_content`, '<strong>', -1), '</strong>', 1)
WHERE `post_title` IS  NULL