mysql - 将 Substring_Index 与第二个表中的数据一起使用
问题描述
作为我上一个问题的后续行动:
我想选择wp_posts
表中匹配的所有内容:
post_type = "answer"
post_type = "question"
- post_type 包含修订,前面是前面任一标准的 ID。例如:
21-revision-v1
或者10903-revision-v1
我想选择那些帖子的第一个数字部分与前2个要求中选择的帖子的ID匹配的帖子。
我现在构建了一个新表ap_qa
,其中包含符合上述条件 1 或 2 的帖子的所有 ID。
现在选择匹配条件 3 的案例,我认为使用Substring_Index()
as 允许在字符串中进行匹配。
我目前的代码是:
SELECT *
FROM `wp_posts` p
WHERE p.post_type IN ('answer', 'question') OR
Substring_Index(p.post_Type,'-revision',1) IN QA.ID
where 之后的第一条规则是满足条件 1 和 2,最后一行是满足条件 3。但是我的语法无效,如返回。
错误消息显示(荷兰语):
#1064 - Er is iets fout in de gebruikte syntax bij 'QA.ID' in regel 4
解决方案
我现在构建了一个新表 ap_qa,其中包含来自符合上述条件 1 或 2 的帖子的所有 ID。
你根本不需要一个临时表。您可以在单个查询中直接从原始表中获得所需的结果:
select *
from wp_posts wp
where post_type in ('answer', 'question') and exists (
select 1
from wp_posts wp1
where
wp1.post_type in ('answer', 'question')
or wp1.id = substring_index(wp.post_type, '-revision', 1)
)
推荐阅读
- apache-flink - Apache Flink:如何使用 Table API 对每 n 行进行分组?
- android - 如何在嵌套的 android 片段上应用 MVP?
- asp.net - 将 Angular SPA 添加到现有的 .net Core MVC 应用程序中
- sql - 在 where 条件中添加 case 语句并在 then 子句中提供条件语句
- angular - Coverity 是否支持 Angular2 及以上版本?
- java - 如何将 AlertDialog 的返回值用于布尔条件
- python - 下载通过 Gmail API 共享的 Google Drive 文件
- stdout - 在Linux中将stdout连接到同一进程的stdin
- batch-file - 创建日志的批处理文件不起作用,为什么?
- unity3d - 团结 | 如何根据 Inspector 上的主要枚举选择更改枚举