Wordpress - 关于两个自定义字段的 SQL 查询问题

发布于 2024-09-28 19:26:44 字数 1963 浏览 8 评论 0原文

到目前为止,我已经得到了这个查询:


if(!empty($_SESSION['s_property_region'])) {
    $sqlWHERE .= " AND $wpdb->postmeta.meta_key = 'property_region' AND $wpdb->postmeta.meta_value = '".$_SESSION['s_property_region']."'";
}
if(!empty($_SESSION['s_property_bedrooms'])) {
    $sqlWHERE .= " AND $wpdb->postmeta.meta_key = 'property_bedrooms' AND $wpdb->postmeta.meta_value = '".$_SESSION['s_property_bedrooms']."'";
}
$sql    =   "SELECT $wpdb->posts.ID
             FROM $wpdb->posts
             LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
             WHERE $wpdb->posts.post_status = 'publish'
             AND $wpdb->posts.post_type = 'property'
             ".$sqlWHERE."
             ORDER BY $wpdb->posts.post_date DESC";

如果我只搜索一个自定义字段而不是同时搜索两个字段,它就会起作用。我知道为什么,这是因为 LEFT Join 的工作方式。不会有一行 post_type 可以同时是两个东西。

连接后我将得到这样的结构:

|   ID   |   meta_key          |   meta_value   |
|   1    |   property_region   |   East         |
|   1    |   property_bedrooms |   4            |
|   2    |   property_region   |   West         |
|   2    |   property_bedrooms |   2            |

是否有一个查询可以将两个表变成这样的结构:

|   ID   |   property_region   |   property_bedrooms   |
|   1    |   East              |   4                   |
|   2    |   West              |   2                   |

如果查询可以使表看起来像上面那样,那么我的简单 WHERE this=that AND this=that 将工作。

对此事的任何帮助都非常感谢,

谢谢

斯科特

编辑:经过一番搜索后,我现在将这段代码放在一起:http://pastebin。 com/5YYDLyeR 它可以工作并返回我想要的结构,但是当我尝试在 property_region 和 property_bedrooms 上执行 WHERE 时,我得到: #1054 - “where 子句”中的未知列“property_bedrooms”

编辑2:好的我已经采用了 PMV 代码,并且为了能够执行 WHERE 子句,我必须执行子查询: http:// astebin.com/dvTgsU7S 这段代码做了我想要的,但是有没有更好的方法来实现这个查询?我认为当数据库中有 1000 多个条目时,性能将成为一个问题......

I've got this query so far:


if(!empty($_SESSION['s_property_region'])) {
    $sqlWHERE .= " AND $wpdb->postmeta.meta_key = 'property_region' AND $wpdb->postmeta.meta_value = '".$_SESSION['s_property_region']."'";
}
if(!empty($_SESSION['s_property_bedrooms'])) {
    $sqlWHERE .= " AND $wpdb->postmeta.meta_key = 'property_bedrooms' AND $wpdb->postmeta.meta_value = '".$_SESSION['s_property_bedrooms']."'";
}
$sql    =   "SELECT $wpdb->posts.ID
             FROM $wpdb->posts
             LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
             WHERE $wpdb->posts.post_status = 'publish'
             AND $wpdb->posts.post_type = 'property'
             ".$sqlWHERE."
             ORDER BY $wpdb->posts.post_date DESC";

It works if I just search for one custom field but not the two together. I know why and its because the way the LEFT Join works. There isnt going to be a row where post_type can be two things at once.

after the join Im going to get a structure like this:

|   ID   |   meta_key          |   meta_value   |
|   1    |   property_region   |   East         |
|   1    |   property_bedrooms |   4            |
|   2    |   property_region   |   West         |
|   2    |   property_bedrooms |   2            |

is there a query that can turn the two tables into a structure like this:

|   ID   |   property_region   |   property_bedrooms   |
|   1    |   East              |   4                   |
|   2    |   West              |   2                   |

If the query can make the table look like above then my simple WHERE this=that AND this=that will work.

Any help on this matter is greatly appreciated

Thanks

Scott

EDIT: After some searching about I now put this code together: http://pastebin.com/5YYDLyeR It works and returns the structure I'm after but as soon as I try to do a WHERE on property_region and property_bedrooms I get: #1054 - Unknown column 'property_bedrooms' in 'where clause'

EDIT2: Ok I have taken PMV code and to be able to do my WHERE clause I've had to do a sub-query: http://pastebin.com/dvTgsU7S This code does what I want but are there any better ways of achieving this query? I presume performance will be an issue when I have 1000+ entries in the DB...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2024-10-05 19:26:44

将整个查询输入如下所示:

SELECT ID
,      MAX(CASE WHEN meta_key = 'property_region' THEN meta_value END) AS property_region
,      MAX(CASE WHEN meta_key = 'property_bedrooms' THEN meta_value END) AS property_bedrooms
FROM (your query)
GROUP BY ID
HAVING MAX(CASE WHEN meta_key = 'property_region' THEN meta_value END) = 'east'
AND    MAX(CASE WHEN meta_key = 'property_bedrooms' THEN meta_value END) = 4

编辑:HAVING 子句。它很丑,但我想这就是你要找的。你仍然需要在 php 代码中构建它,但我认为它应该给你你想要的。

Feed your entire query into something like this:

SELECT ID
,      MAX(CASE WHEN meta_key = 'property_region' THEN meta_value END) AS property_region
,      MAX(CASE WHEN meta_key = 'property_bedrooms' THEN meta_value END) AS property_bedrooms
FROM (your query)
GROUP BY ID
HAVING MAX(CASE WHEN meta_key = 'property_region' THEN meta_value END) = 'east'
AND    MAX(CASE WHEN meta_key = 'property_bedrooms' THEN meta_value END) = 4

EDIT: The HAVING clause. It's ugly, but I think it's what you're looking for. You'll still have to build it in the php code, but I think it should give you what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文