组合多个 WordPress 数据库查询

发布于 2024-10-02 14:31:36 字数 884 浏览 5 评论 0原文

请原谅我重新措辞并重新提出这个问题,但是我几周前收到的答案并没有多大帮助......

基本上,我希望以某种方式组合 WordPress 中的多个数据库查询,通过搜索来检索用户 ID对于“usermeta”表中的术语,但仅限具有特定“meta_value”的条目

我正在尝试组合:

$users = $wpdb->get_results("SELECT user_id, meta_value as 'business_name' 
FROM $wpdb->usermeta
WHERE meta_key = 'business_name'");

AND:

$users = $wpdb->get_results("SELECT user_id, meta_value as 'business_description' 
FROM $wpdb->usermeta
WHERE meta_key = 'business_description'");

本质上是这样的:

$users = $wpdb->get_results("SELECT user_id, business_name, business_description 
FROM 
WHERE
    business_name LIKE '%{$keyword}%' OR
    business_description LIKE '%{$keyword}%'");

我研究过 INNER JOIN 和子查询,但似乎找不到好的解决方案。我意识到我可以逃避多个查询,但这可能会搜索数千个条目,所以我想尽可能地优化它。

Forgive me for re-wording and re-asking this question, but the answers I received a couple weeks back didn't help much...

Basically, I'm looking to somehow combine multiple database queries in Wordpress to retrieve user IDs by searching for term in the 'usermeta' table, but only entries that have a certain 'meta_value'

I'm trying to combine:

$users = $wpdb->get_results("SELECT user_id, meta_value as 'business_name' 
FROM $wpdb->usermeta
WHERE meta_key = 'business_name'");

AND:

$users = $wpdb->get_results("SELECT user_id, meta_value as 'business_description' 
FROM $wpdb->usermeta
WHERE meta_key = 'business_description'");

To essentially have this:

$users = $wpdb->get_results("SELECT user_id, business_name, business_description 
FROM 
WHERE
    business_name LIKE '%{$keyword}%' OR
    business_description LIKE '%{$keyword}%'");

I've looked into INNER JOINs and subqueries, but cannot seem to find a good solution. I realize that I can get away with multiple queries, but this would be searching through possibly thousands of entries, so I'd like to optimize it as much as possible.

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

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

发布评论

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

评论(2

厌味 2024-10-09 14:31:36

@John:

如果我理解你的问题是正确的(因为你给出了你正在尝试的技术示例,但没有描述你想要实现的最终结果,我不确定)您似乎正在进行用户搜索?如果是,下面是我认为您需要的。

<?php

  // Load WordPress, but only for standalone example use
  include '../wp-load.php';

  // Make sure the database object is available
  global $wpdb;

  // Get value entered by the user
  $keyword = $_GET['keyword'];

  // This would be used in your code; the percent would confuse prepare() below
  $keyword = "%{$keyword}%";

  // Join each meta field as a separate join and reference the meta_key in the join
  // prepare() below replaces "%s" with a quoted string value
  $sql =<<<SQL
SELECT
  users.user_nicename AS user_name,
  bizname.meta_value AS business_name,
  bizdesc.meta_value AS business_description
FROM
  {$wpdb->users} AS users
  INNER JOIN {$wpdb->usermeta} AS bizname
    ON bizname.user_id=users.ID
   AND bizname.meta_key='business_name'
  INNER JOIN {$wpdb->usermeta} AS bizdesc
    ON bizdesc.user_id=users.ID
   AND bizdesc.meta_key='business_description'
WHERE 1=0
  OR bizname.meta_value LIKE %s
  OR bizdesc.meta_value LIKE %s
SQL;

  // User prepare() to avoid SQL injection hacks
  $sql = $wpdb->prepare($sql,$keyword,$keyword);

  // Finally, get yer results!
  $users = $wpdb->get_results($sql);
  echo "<ul>";
  foreach($users as $user) {
    echo "<li>User= {$user->user_name}, Business= {$user->business_name}:{$user->business_description}</li>";
  }
  echo "<ul>";

上面是一个完整的工作示例,您可以将其复制到网站根目录中的文件中,并将其命名为 /test.php ,这样您就可以使用如下 URL 来查看它的工作情况:

http://example.com/test.php?keyword=会计

当然,由于构建了查询缓存系统,这有时可能比使用多个查询的性能低一些。 -在 WordPress 中,但如果没有一些基准测试就无法判断。

希望这有帮助。

-迈克

P.S.顺便说一句,我假设你没有意识到这一点,但由于你之前的问题显然没有得到太多 WordPress 的喜爱,这个问题也没有得到太多,我会提到 WordPress Answers 网站,它是 StackOverflow 的姐妹网站。许多 WordPress 爱好者都在那里回答 WordPress 特定的问题。我在 StackOverflow 的经验是,他们拥有一些网络上最好的开发人员,但这里很少有人有使用 WordPress 开发的具体经验,所以你最终会看到这里的人试图回答MySQL 在不了解 WordPress 数据库架构和 WordPress 特定最佳实践的情况下提出问题。在 WordPress Answers 中提问,我认为您的 WordPress 特定问题的答案质量会得到提高。

Hi @John:

If I understand your question correct (since you gave a technical example of what you were attempting but not a description of what end result you were trying to accomplish I'm not sure) it seems you are doing a user search? If yes, below is what I think you need.

<?php

  // Load WordPress, but only for standalone example use
  include '../wp-load.php';

  // Make sure the database object is available
  global $wpdb;

  // Get value entered by the user
  $keyword = $_GET['keyword'];

  // This would be used in your code; the percent would confuse prepare() below
  $keyword = "%{$keyword}%";

  // Join each meta field as a separate join and reference the meta_key in the join
  // prepare() below replaces "%s" with a quoted string value
  $sql =<<<SQL
SELECT
  users.user_nicename AS user_name,
  bizname.meta_value AS business_name,
  bizdesc.meta_value AS business_description
FROM
  {$wpdb->users} AS users
  INNER JOIN {$wpdb->usermeta} AS bizname
    ON bizname.user_id=users.ID
   AND bizname.meta_key='business_name'
  INNER JOIN {$wpdb->usermeta} AS bizdesc
    ON bizdesc.user_id=users.ID
   AND bizdesc.meta_key='business_description'
WHERE 1=0
  OR bizname.meta_value LIKE %s
  OR bizdesc.meta_value LIKE %s
SQL;

  // User prepare() to avoid SQL injection hacks
  $sql = $wpdb->prepare($sql,$keyword,$keyword);

  // Finally, get yer results!
  $users = $wpdb->get_results($sql);
  echo "<ul>";
  foreach($users as $user) {
    echo "<li>User= {$user->user_name}, Business= {$user->business_name}:{$user->business_description}</li>";
  }
  echo "<ul>";

The above is a complete working example you can copy to a file in the root of your website and call it something like /test.php allowing you to see it work by using a URL like this:

http://example.com/test.php?keyword=Accounting

Of course, this may be less performant at times than using multiple queries because of the query caching systems built-in to WordPress but it's impossible to tell without some benchmarking.

Hope this helps.

-Mike

P.S. By the way, I'm assuming you were not aware of it but since your prior question evidently hadn't gotten much WordPress love nor had this one I'll mention the WordPress Answers website which is a sister site to StackOverflow. Lots of WordPress enthusiasts are on hand over there to answer WordPress-specific questions.My experience with StackOverflow is they have some of the best developers on the web but few here have specific experience developing with WordPress so you end up with people here trying to answer MySQL questions without knowing the WordPress database schema and without knowing WordPress-specific best practices. Ask over at WordPress Answers and I think you'll an improved quality of answers to your WordPress-specific questions.

差↓一点笑了 2024-10-09 14:31:36

您能否发布有关您收到的错误消息的一些详细信息?这里的 SQL 看起来没问题,但是如果不看到错误消息就无法判断问题出在哪里。

例如,您确定正在填充 $keyword 字段吗?返回多少个结果?您是否尝试过不使用 WHERE 语句或仅使用单个 OR 来排除故障?另外,您是否尝试过使用一些测试关键字直接在服务器上运行此 SQL?

请让我们更多地了解您尝试解决问题的方法,我们将能够提供进一步的帮助。

亲切的问候
卢克·彼得森

Can you post some details on the error messages you are receiving? The SQL here looks ok, but it's impossible to tell what the problem is without seeing the error messages.

For example, are you sure the $keyword field is being populated? How many results to return? Have you tried it without the WHERE statement or just a single OR to troubleshoot? Also, have you tried running this SQL directly on your server with some test keywords?

Give us more of an idea on what you have tried to fix the problem and we'll be able to assist further.

Kind Regards
Luke Peterson

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