简体   繁体   English

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

[英]Complex SQL query, involving three tables and variable table names

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 最后,为我提供了以下POST数据

state

Here's what I need to do: Get the name of any blogs in the area of the POST data. 这是我需要做的:获取POST数据区域中任何博客的名称。 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. 为了更简洁地说,我需要在与option_name='state'option_value="$_POST['state']'相同的表上选择option_name 'blog_name'option_value ,并且表名是从blog_id列表中blog_id (来自表格blogs ),并在其末尾附加“ _options”。

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. 无论如何,我认为应在任何地方查询stackoverflow。

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. 顺便说一句,这是因为我正在使用Wordpress MU,并且选择在各种Blog的动态创建的表上放置一些额外的设置。

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. 您可以在直接SQL中执行此操作,但我认为最好使用内置的wordpress函数,这样,如果数据库结构发生了根本改变(由于MU和常规WP内核将很快合并,可能会发生这种情况),还可以。

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. 这将为您提供MU安装上所有活动博客的详细信息列表以及选项表中的“状态”字段。

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. 如果您需要在页面加载中多次使用此信息,请使用WP的对象缓存来存储变量以供以后使用。 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. 您还可以通过多种方式通过父博客通过ajax或Web服务调用它,或者实现内存缓存解决方案,以便在出现问题时可以集中存储和管理这些数据,但是我认为如果使用对象在前端缓存诸如WP Super Cache之类的东西就可以了。

I don't think you're doing this the right way. 我认为您的做法不正确。

Instead of using the table name _options, why not have something like 与其使用表名_options,不如使用

blog_options

Which contains the fields 其中包含字段

  • blog_id blog_id
  • option_id option_id
  • option_name option_name
  • option_value option_value

Then you can happily JOIN based on blog_id 然后,您可以根据blog_id高兴地JOIN

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? 为什么不只创建一个包含变量字段的表,其中包含varchar类型的变量名? That field could contain the $blog_id. 该字段可以包含$ blog_id。

Just like the other answer on your question. 就像您问题上的其他答案一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM