简体   繁体   English

FOREACH sql表项从另一个表获取结果

[英]FOREACH sql table entry get result from another table

I'm working with vBulletin forums and I'm trying to get results from one table if it exists in another table. 我正在使用vBulletin论坛,我试图从一个表中获取结果,如果它存在于另一个表中。

This is the creation of the table the data is from 这是数据来源的表的创建

$db->query("ALTER TABLE " . TABLE_PREFIX . "drc_settings ADD thread_ids varchar(50) NOT NULL default '0'");

I have created an input field meant for comma separated numbers (thread ID's). 我创建了一个输入字段,用于逗号分隔的数字(线程ID)。 That submits to this column, so a result in this column would look like: 提交到此列,因此此列中的结果如下所示:

1,46,23

Another column in this table is threadid, this is the thread the post was submitted from. 这个表中的另一列是threadid,这是帖子提交的帖子。

So this is what we have in the drc_settings table 所以这就是我们在drc_settings表中的内容

threadid  thread_ids
   5       1,46,23

Now another table that exists is thread, in the thread table we have: 现在另一个存在的表是thread,在我们的线程表中:

threadid  title
   46     Some Title

What I need to know is, how do I get the title from thread if thread_ids or drc_settings match threadid of thread? 我需要知道的是,如果thread_ids或drc_settings与线程的threadid匹配,如何从线程获取标题?

This is where I'm at with it, but I'm a little out of my element on this one. 这就是我所处的位置,但我对这个问题的看法有些偏差。

$res = $post['thread_ids'];
$res = explode (',', $res);
$post['drcid'] = '<ul>';

foreach ( $res as $ret ) {
$db->query("
  SELECT
  thread.threadid, drc.threadid AS threadid,
  thread.threadid, thread.title AS threadtitle
  FROM " . TABLE_PREFIX . "thread AS thread
  LEFT JOIN " . TABLE_PREFIX . "drc_settings AS drc ON (drc.threadid=thread.threadid)
  WHERE thread.threadid VAR (" . $ret . ")
");  
$post['drcid'] .= '<li><a href="showthread.php?t=' . $ret . '">'.   $thread[threadtitle] .'</a></li>';
}
$post['drcid'] .= '</ul>';

If I remove the query from the foreach, I am able to get $res to return 1 46 23 which is stumping me why the query isn't working properly. 如果我从foreach中删除查询,我可以获得$ res返回1 46 23,这让我觉得查询无法正常工作。

The end result will be a list of links to each thread specified in the input, IE: 最终结果将是输入IE中指定的每个线程的链接列表:

  • Some Title 一些标题
  • Another thread 另一个线程
  • Thread Title 主题标题

This is the error returned with the current setup I have 这是我当前设置返回的错误

Database error in vBulletin 3.8.9:

Invalid SQL:

                SELECT
        thread.threadid, drc.threadid AS threadid,
                    thread.threadid, thread.title AS threadtitle
                FROM thread AS thread
                LEFT JOIN drc_settings AS drc ON (drc.threadid=thread.threadid)
                WHERE thread.threadid VAR (1);

MySQL Error   : You have an error in your SQL syntax;

Using $ret (the parent threadid)... 使用$ ret(父级线程)...

$query="SELECT B.threadid,B.title 
    FROM drc.settings A 
    LEFT JOIN thread B 
    ON FIND_IN_SET(B.threadid,A.threadids) 
    WHERE A.threadid={$ret};"

Then loop through the resultset as needed to build the $post array. 然后根据需要循环遍历结果集以构建$ post数组。

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

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