table wp_posts +-----+----------+ | ID |post_type | +-----+----------+ | 1 | A | | 2 | A | | 3 | A | | 3 | B | | 3 | C | +-----+----------+
table wp_term_relationships (object_id = wp_posts.ID) +-----------+------------------+ | object_id | term_taxonomy_id | +-----------+------------------+ | 1 | 1 | | 2 | 3 | | 2 | 2 | | 1 | 2 | | 3 | 1 | +-----------+-------------------+
table wp_esp_datetime (EVT_ID = wp_posts.ID) +-----------+---------------------+ | EVT_ID | DTT_EVT_end | +-----------+---------------------+ | 1 | 2015-12-01 16:00:00 | | 2 | 2015-12-01 16:00:00 | | 3 | 2015-12-01 16:00:00 | +-----------+---------------------+
现在我想做的是: 使用wp_esp_datetime中的左连接日期时间查询来自wp_posts的所有内容,但仅查找在term_taxonomy_id = 3的wp_term_relationships中没有条目的人。 我的问题是,在wp_term_relationships.object_id
中wp_post.ID可以多次我当前的查询如下所示:
SELECT * FROM wp_posts
INNER JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id
LEFT JOIN wp_esp_datetime on wp_posts.ID = wp_esp_datetime.EVT_ID
WHERE wp_posts.post_type = 'A' GROUP BY wp_posts.ID
所以问题是:
如何在wp_posts中排除wp_postss中包含term_taxonomy_id = 3的条目的行,即使与另一个term_taxonomy(不是3)匹配?
答案 0 :(得分:1)
添加(not)exists子句
SELECT * FROM wp_posts p
INNER JOIN wp_term_relationships tr on p.ID = tr.object_id
LEFT JOIN wp_esp_datetime ed on p.ID = ed.EVT_ID
WHERE p.post_type = 'A'
and not exists (select null
from wp_term_relationships
where term_taxonomy_id = 3
and p.ID = object_id)
GROUP BY p.ID
答案 1 :(得分:0)
尝试添加子查询?
AND wp_posts.ID NOT IN
(SELECT wtr2.object_id FROM wp_term_relationships wtr2 WHERE wtr2.term_taxonomy_id = 3)
您的查询已修改:
SELECT *
FROM wp_posts p
INNER JOIN wp_term_relationships wtr on p.ID = wtr.object_id
LEFT JOIN wp_esp_datetime wed on p.ID = wed.EVT_ID
WHERE p.post_type = 'A'
AND p.ID NOT IN (SELECT wtr2.object_id FROM wp_term_relationships wtr2 WHERE wtr2.term_taxonomy_id = 3)
GROUP BY p.ID
答案 2 :(得分:0)
使用not exists
并使用 wp_term_relationships 删除联接:
SELECT p.ID, ed.DTT_EVT_end
FROM wp_posts p
LEFT JOIN wp_esp_datetime ed on p.ID = ed.EVT_ID
WHERE p.post_type = 'A'
AND NOT EXISTS (
SELECT 1
FROM wp_term_relationships
WHERE term_taxonomy_id = 3
AND p.ID = object_id)
ORDER BY p.ID, ed.DTT_EVT_end;
答案 3 :(得分:0)
我无法看到以下问题无法解决的问题......
+----+-----------+--------+---------------------+
| ID | post_type | EVT_ID | DTT_EVT_end |
+----+-----------+--------+---------------------+
| 1 | A | 1 | 2015-12-01 16:00:00 |
| 3 | A | 3 | 2015-12-01 16:00:00 |
| 3 | B | 3 | 2015-12-01 16:00:00 |
| 3 | C | 3 | 2015-12-01 16:00:00 |
+----+-----------+--------+---------------------+
这将产生以下结果......
{{1}}
将GROUP BY子句应用于此结果集似乎是荒谬的。