gpt4 book ai didi

mysql - 左连接不起作用

转载 作者:行者123 更新时间:2023-11-30 22:03:47 24 4
gpt4 key购买 nike

我有 2 个分类法:citystreet

我需要为我的一个插件获取 postmeta

只有一个分类的工作代码:

SELECT *
FROM $wpdb->postmeta
LEFT JOIN $wpdb->posts ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON ($wpdb->postmeta.post_id = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE meta_key LIKE 'rwp_rating%'
AND ($wpdb->term_taxonomy.taxonomy = 'city'
AND $wpdb->term_taxonomy.term_id = '2')

但是如果我追加

AND ($wpdb->term_taxonomy.taxonomy = 'street' AND $wpdb->term_taxonomy.term_id = '5')

它返回空结果。

我也尝试这样做:

SELECT *
FROM $wpdb->postmeta
LEFT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON ($wpdb->postmeta.post_id = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy AS t1 ON ($wpdb->term_relationships.term_taxonomy_id = t1.term_taxonomy_id)
LEFT JOIN $wpdb->term_taxonomy AS t2 ON ($wpdb->term_relationships.term_taxonomy_id = t2.term_taxonomy_id)
WHERE meta_key LIKE 'rwp_rating%'
AND (t1.taxonomy = 'city'
AND t1.term_id = '2')
AND (t2.taxonomy = 'street'
AND t2.term_id = '3331');

而且它也不起作用。

请注意:我需要同时匹配两者,例如:city = 2 AND street = 2
不是:city = 2 OR street = 2

最佳答案

您的第一次尝试不会成功,因为单个分类记录不能同时包含街道和城市。

将您的第二次尝试更新为:

SELECT * FROM $wpdb->postmeta 
LEFT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
LEFT JOIN $wpdb->term_relationships ON ($wpdb->postmeta.post_id = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy as t1 ON ($wpdb->term_relationships.term_taxonomy_id = t1.term_taxonomy_id AND t1.taxonomy = 'city')
LEFT JOIN $wpdb->term_taxonomy as t2 ON ($wpdb->term_relationships.term_taxonomy_id = t2.term_taxonomy_id AND t2.taxonomy = 'street')
WHERE meta_key LIKE 'rwp_rating%' AND t1.term_id = '2' AND t2.term_id = '3331');

关于mysql - 左连接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42429557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com