mysql - 根据公共(public) ID 将三个表连接在一起

标签 mysql sql

enter image description here

我想要生成的是 wp_tylerposts 表,它具有与查询相同的规范(仅选择带有 post_type='sponsor' 的表,但我想引用其他两个表(wp_tylerterm_relationships 中的object_idwp_tylerterms 中相应的name)因此,添加到 wp_tylerpost 的将是 wp_tylerterms 中相应的名称值(例如,添加到表中的是“金牌赞助商”)。

希望这是有道理的。我确信有一个非常简单的解决方案,并且我已经尝试过一些连接查询,但没有任何运气......很长一段时间没有做过任何事情。任何帮助将不胜感激!

编辑:我认为我已经接近了,但我仍然无法从 wp_tylerterms 检索“name”列值,这就是我所拥有的:

SELECT 
    c.ID, c.post_title, a.name
FROM 
    wp_tylerposts c
LEFT JOIN
    wp_tylerterm_relationships b
ON
    c.ID = b.object_id
LEFT JOIN
    wp_tylerterms a
ON
    b.object_id = a.term_id
WHERE
    c.post_type = 'sponsor'

最佳答案

在第二个 join 中,您使用 b.object_id 而不是 b.term_taxonomy_id。您的查询应如下所示:

SELECT 
    c.ID, c.post_title, a.name
FROM 
    wp_tylerposts c
LEFT JOIN
    wp_tylerterm_relationships b
ON
    c.ID = b.object_id
LEFT JOIN
    wp_tylerterms a
ON
    b.term_taxonomy_id = a.term_id
WHERE
    c.post_type = 'sponsor'

关于mysql - 根据公共(public) ID 将三个表连接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34756770/

相关文章:

mysql - 如何对 NULL 字段进行分组

sql - Postgres 约束和外键

java - #1411 - 不正确的日期时间值 : '11:21:21' for function str_to_date

SQL 逻辑中的 Java 插入或更新

mysql - 多表Mysql JOIN

Mysql过程,If语句

php - 使用 MySQL 数据检查目录名称 (PHP/HTML)

php - 将 MySQL JSON_OBJECT() 与参数化查询结合使用

sql - 如何填充引用另一个表的列 ID 的空表行?

sql - 数据类型 varchar 和 varbinary(max) 在 add 运算符中不兼容