如何左联接2个字段匹配的2个表

时间:2019-03-24 13:50:45

标签: mysql datatable left-join

我要从中传输数据  cms_nodeinfo.descriptionwp_postmeta.meta_value 仅在情况下cms_nodeinfo.nodeid **MATCHES** wp_postmeta.post_id AND (如果wp_postmeta.meta_key **EQUALS** '_yoast_wpseo_metadesc'

这是我尝试的查询示例:

SELECT
    cms_nodeinfo.description, wp_postmeta.meta_value
FROM
    cms_nodeinfo, wp_postmeta
WHERE
    cms_nodeinfo.nodeid = wp_postmeta.post_id
    wp_postmeta.meta_key = _yoast_wpseo_metadesc 

ps。伙计们,我不是编码员,我正在学习,请耐心等待。谢谢。

好的,我想是这样,但是我需要有关可以粘贴到查询中的实际语法的帮助:

SELECT cms_nodeinfo.description 
FROM cms_nodeinfo 
LEFT JOIN wp_postmeta ON cms_nodeinfo.description = wp_postmeta.meta_value 
WHERE wp_postmeta.meta_key = _yoast_wpseo_metadesc AND cms_nodeinfo.nodeid = wp_postmeta.post_id

我也尝试过

SELECT
   cms_nodeinfo.description
FROM
   cms_nodeinfo
LEFT JOIN cms_nodeinfo.description ON
   wp_postmeta.meta_value
WHERE
   wp_postmeta.meta_key = _yoast_wpseo_metadesc AND cms_nodeinfo.nodeid = wp_postmeta.post_id

但是返回了一个错误,说

Table 'cms_nodeinfo.description' doesn't exist

表名称为cms_nodeinfo和wp_postmeta。目标是将cms_nodeinfo中的description字段转移到wp_postmeta表的meta_value字段中。

我也尝试过:

SELECT
    `description`
FROM
    cms_nodeinfo
LEFT JOIN wp_postmeta.meta_value WHERE // LINE 5
    wp_postmeta.meta_key = _yoast_wpseo_metadesc AND cms_nodeinfo.nodeid = wp_postmeta.post_id 

但是返回以下错误:

 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE
    wp_postmeta.meta_key = _yoast_wpseo_metadesc AND cms_nodeinfo.nodeid ' at line 5 

我也尝试过:

SELECT
    `description`
FROM
    cms_nodeinfo
WHERE
    wp_postmeta.meta_key = `_yoast_wpseo_metadesc` AND cms_nodeinfo.nodeid = wp_postmeta.post_id
LEFT JOIN cms_nodeinfo.description ON //LINE7
    wp_postmeta.meta_value


but that returns:
 check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LEFT JOIN cms_nodeinfo.description ON
    wp_postmeta.meta_value LIMIT 0, 25' at line 7 (marked)

我也尝试过:

SELECT `cms_nodeinfo`.`description`, `wp_postmeta`.`meta_key`, `cms_nodeinfo`.`nodeid`
FROM `cms_nodeinfo`, `wp_postmeta`
LEFT JOIN `wp_postmeta`.`meta_value`
WHERE `wp_postmeta`.`meta_key` = '_yoast_wpseo_metadesc' AND `cms_nodeinfo`.`nodeid` = `wp_postmeta`.`post_id`;  //LINE4


which returns a syntax error in line 4 (marked)

我也尝试过:

SELECT
    `cms_nodeinfo`.`description`,
    `wp_postmeta`.`meta_key`,
    `cms_nodeinfo`.`nodeid`
FROM
    `cms_nodeinfo`,
    `wp_postmeta`
LEFT JOIN `cms_nodeinfo.description`
  ON `wp_postmeta`.`meta_value` 
 WHERE
    `wp_postmeta`.`meta_key` = '_yoast_wpseo_metadesc' AND `cms_nodeinfo`.`nodeid` = `wp_postmeta`.`post_id`;

我也尝试过:

REPLACE `meta_value` WITH `cms_nodeinfo`.`description`, WHERE `wp_postmeta`.`meta_key` = '_yoast_wpseo_metadesc' AND `cms_nodeinfo`.`nodeid` = `wp_postmeta`.`post_id`;

@Martin建议:

  

从tbl左联接x上x.a = tbl.a和x.b = tbl.b

所以我在这里尝试过

FROM                 //POSITION 0
    `cms_nodeinfo`,
LEFT JOIN `cms_nodeinfo.description`
ON `wp_postmeta`.`meta_value` 
 WHERE
    `wp_postmeta`.`meta_key` = '_yoast_wpseo_metadesc' AND `cms_nodeinfo`.`nodeid` = `wp_postmeta`.`post_id`;


which returned the error:
Unrecognized statement type. (near "FROM" at position 0)

也许我使用的连接类型错误?我也尝试了INNER JOIN,但这也不起作用。

SELECT
    `cms_nodeinfo`.`description`,
    `wp_postmeta`.`meta_key`,
    `cms_nodeinfo`.`nodeid`
FROM
    `cms_nodeinfo`,
    `wp_postmeta`
INNER JOIN `wp_postmeta`.`meta_value` WHERE
    `wp_postmeta`.`meta_key` = '_yoast_wpseo_metadesc' AND `cms_nodeinfo`.`nodeid` = `wp_postmeta`.`post_id`;

好的,这个程序成功运行,但是没有数据返回

SELECT
    `cms_nodeinfo`.`description`
FROM
    `cms_nodeinfo`
LEFT JOIN `wp_postmeta` ON `wp_postmeta`.`meta_value` = `cms_nodeinfo`.`description`
WHERE
    `wp_postmeta`.`meta_key` = '_yoast_wpseo_metadesc' AND `cms_nodeinfo`.`nodeid` = `wp_postmeta`.`post_id`;  

我还尝试了RIGHT Join,INNER和FULL,但所有这些都失败了。该字段在那里,但是我无法获取要填充的数据。

有什么想法吗?请帮助?!

1 个答案:

答案 0 :(得分:0)

您可以在设置的条件下进行更新:

update 
  wp_postmeta inner join cms_nodeinfo
  on cms_nodeinfo.nodeid = wp_postmeta.post_id
set
  wp_postmeta.meta_value = cms_nodeinfo.description
where wp_postmeta.meta_key = '_yoast_wpseo_metadesc'