Php 如何从同一列和表中搜索两个元数据
我在WordPress中进行自定义搜索。有三个字段可供搜索Php 如何从同一列和表中搜索两个元数据,php,mysql,sql,wordpress,Php,Mysql,Sql,Wordpress,我在WordPress中进行自定义搜索。有三个字段可供搜索 年龄 位置 职称 从wp\u posts>Post\u Title字段中搜索帖子标题,位置和年龄是自定义元字段y\u age,y\u activity\u locations。所以有6种搜索场景 如果用户输入: 输入标题或 选择位置或 选择年龄或 输入标题并选择位置或位置 输入标题并选择年龄或 选择位置并选择年龄 我的前五(5)个方案运行良好,但我的第六个方案不起作用,因为它来自同一个表(wp_postmeta)和同一列,但有两个不同
wp\u posts>Post\u Title
字段中搜索帖子标题,位置和年龄是自定义元字段y\u age,y\u activity\u locations
。所以有6种搜索场景
如果用户输入:
wp_postmeta
)和同一列,但有两个不同的值
所以我尝试了这个查询:
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
And y_meta.meta_key = 'y_age'
And y_meta.meta_value Like '%3 to 5%'
查询不起作用,因为我认为sever混淆了同一列中的两个值
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
Or y_meta.meta_key = 'y_age'
Or y_meta.meta_value Like '%3 to 5%'
这个查询有效,它只提供年龄或位置数据,但我希望同时搜索这两个值
我还尝试了子查询(以前从未尝试过)
但它给了我这个错误
1241-操作数应包含1列
所以请指导我如何从同一列中获得两个值
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%'
Or y_meta.meta_key = 'y_age'
Or y_meta.meta_value Like '%3 to 5%'
请仅提供查询建议,而不提供任何其他解决方案。请尝试以下查询:
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And (
(y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%')
Or (y_meta.meta_key = 'y_age'
And y_meta.meta_value Like '%3 to 5%')
)
请尝试以下查询:
Select y_posts.*, y_meta.*
From wp_posts As y_posts
Inner Join wp_postmeta As y_meta
On y_posts.ID = y_meta.post_id
Where y_posts.post_type = 'download'
And y_posts.post_status = 'publish'
And (
(y_meta.meta_key = 'y_activity_locations'
And y_meta.meta_value Like '%Armidale%')
Or (y_meta.meta_key = 'y_age'
And y_meta.meta_value Like '%3 to 5%')
)
你试试,wordpress查询
<?php
$argsCat = array(
'posts_per_page' => -1,
'post_type' => 'download',
'meta_key' => 'y_activity_locations',
'meta_value' => '%Armidale%',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'y_age',
'value' => '3',
'compare' => '>='
),
'relation' => 'AND',
array(
'key' => 'y_age',
'value' => '5',
'compare' => '<='
)
),
);
$normal_array = get_posts($argsCat);
?>
您可以尝试,wordpress查询
<?php
$argsCat = array(
'posts_per_page' => -1,
'post_type' => 'download',
'meta_key' => 'y_activity_locations',
'meta_value' => '%Armidale%',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'y_age',
'value' => '3',
'compare' => '>='
),
'relation' => 'AND',
array(
'key' => 'y_age',
'value' => '5',
'compare' => '<='
)
),
);
$normal_array = get_posts($argsCat);
?>
如果你只想要查询建议,那么为什么要用php标记它?我的意思是没有插件建议如果你只想要查询建议,那么为什么要用php标记它?我的意思是没有插件建议你的答案工作得很好。。。。请在你的回答和你的帖子中告诉我这个逻辑。帖子状态='publish'和()。。。为什么我们将我们的元键和值包装在另一个()中。。。几分钟后我接受你的回答。。。thanx:)非常欢迎并感谢你们的投票。我有包装的meta_键和meta_值,因为我们需要和它们之间的条件,我们需要或条件的组合,根据您的要求。您的答案工作完美。。。。请在你的回答和你的帖子中告诉我这个逻辑。帖子状态='publish'和()。。。为什么我们将我们的元键和值包装在另一个()中。。。几分钟后我接受你的回答。。。thanx:)非常欢迎并感谢你们的投票。我有包装的meta_键和meta_值,因为我们需要和它们之间的条件,我们需要或它们的组合条件,根据您的要求。