复杂的SQL查询,涉及三张表和变量表名

发布于 2024-08-03 06:29:28 字数 847 浏览 7 评论 0原文

我有两个不同的表,我需要从中提取

blogs

具有以下列的

blog_id

数据和另一个具有变量名称的表,例如

$blog_id . "_options

哪个具有以下列:

option_id, option_name, option_value

例如:

option_id = 1, option_name='state', option_value='Texas'
option_id = 2, option_name='blog_name', option_value='My Blog'

最后,我提供了以下 POST 数据

state

这是我的内容需要做的:获取POST数据区域中任何博客的名称。更简洁地说,我需要在 option_name='state' 和 < code>option_value="$_POST['state']' 并且表名称是根据 blog_id 列表创建的(来自表 blogs) 无论如何,我认为

上帝,我什至不知道我想要做的事情是否可以用人的嘴来表达,

stackoverflow 是一个可以询问的地方,如果有的话

请告诉我 。可以为您澄清任何事情,我会尝试。

顺便说一句,这是因为我正在使用 Wordpress MU,并选择在各种博客的动态创建的表上添加一些额外的设置。

I have two different tables from which I need to pull data

blogs

which has the following column

blog_id

and another table which has a variable name, like

$blog_id . "_options

Which has the following columns:

option_id, option_name, option_value

For example:

option_id = 1, option_name='state', option_value='Texas'
option_id = 2, option_name='blog_name', option_value='My Blog'

And finally, I am provided with the following POST data

state

Here's what I need to do: Get the name of any blogs in the area of the POST data. To put it more succinctly, I need to select the option_value of option_name 'blog_name' on the same table that option_name='state' and option_value="$_POST['state']' and the table names are created from a list of blog_id's (from the table blogs) with '_options' appended to the end.

God I don't even know if what I am trying to do can be said with a human mouth.

Anyways, I figure stackoverflow is the place to ask, if anywhere.

Let me know if I can clarify anything for you, i will try.

By the way this is because I am using Wordpress MU and have opted to put some extra settings on the various blog's dynamically created tables.

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

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

发布评论

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

评论(3

手心的温暖 2024-08-10 06:29:28

你可以在直接 SQL 中做到这一点,但我认为你最好使用内置的 WordPress 函数,这样如果数据库结构发生变化(这可能是因为 MU 和常规 WP 核心很快就会合并)你'还可以。

听起来您希望能够从活动博客中提取有关其他博客的信息。我将分两步完成:

$blogs = get_blog_list(0,'all');
foreach($blogs as &$blog) {
    switch_to_blog($blog['id']);
    $blog['state'] = get_option('state');
    restore_current_blog();
}
restore_current_blog();

这将为您提供 MU 安装上所有活动博客的详细信息列表 + 选项表中的状态字段。

是的,它不太优雅,但功能齐全,没有什么混乱。如果您需要在页面加载中多次使用此信息,请使用 WP 的对象缓存来存储变量以供以后使用。还有很多方法可以通过 ajax 或来自父博客的 Web 服务调用它,或者实现 memcache 解决方案,以便在出现问题时可以集中存储和管理这些数据,但我认为如果您使用该对象在前端使用 WP Super Cache 之类的东西进行缓存应该没问题。

You can do this in direct SQL but I think you'd be better off using built in wordpress functions so that if that DB structure changes at all (which it might since the MU and regular WP cores are set to be merged soon) you're still OK.

It sounds like you want to be able to pull info about other blogs from the active blog. I'd do it in two steps:

$blogs = get_blog_list(0,'all');
foreach($blogs as &$blog) {
    switch_to_blog($blog['id']);
    $blog['state'] = get_option('state');
    restore_current_blog();
}
restore_current_blog();

That'll give you a list of details for all active blogs on the MU install + the state field from the options table.

Yeah, its less than elegant, but its functional with little mess. If you need to use this info multiple times in a page load then use WP's object cache to store the variable for later use. There's also a myriad of ways you could either call this via ajax or web-service from the parent blog or implement a memcache solution so that this data can be centrally stored and managed if this becomes an issue, but I think if you use the object cache here with something like WP Super Cache on the front end you should be fine.

吃兔兔 2024-08-10 06:29:28

我认为你这样做的方式不对。

与其使用表名 _options,为什么不使用类似这样的内容,

blog_options

其中包含字段

  • blog_id
  • option_id
  • option_name
  • option_value

那么你可以愉快地基于 blog_id JOIN

I don't think you're doing this the right way.

Instead of using the table name _options, why not have something like

blog_options

Which contains the fields

  • blog_id
  • option_id
  • option_name
  • option_value

Then you can happily JOIN based on blog_id

贱贱哒 2024-08-10 06:29:28

如果我得到正确的结果,那么您正在创建带有变量名称的表。

我认为这不是一个好主意。

为什么不只创建一个表,其中包含一个变量字段,其中包含 varchar 类型的变量名称?该字段可以包含 $blog_id。

就像你问题的另一个答案一样。

If i get this correct, you are creating tables with variable names.

I don't think that that is a good idea.

Why don't you create just one table with a variable field in it that contains the variable name in type varchar? That field could contain the $blog_id.

Just like the other answer on your question.

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