繁体   English   中英

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

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

我正在使用vBulletin论坛,我试图从一个表中获取结果,如果它存在于另一个表中。

这是数据来源的表的创建

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

我创建了一个输入字段,用于逗号分隔的数字(线程ID)。 提交到此列,因此此列中的结果如下所示:

1,46,23

这个表中的另一列是threadid,这是帖子提交的帖子。

所以这就是我们在drc_settings表中的内容

threadid  thread_ids
   5       1,46,23

现在另一个存在的表是thread,在我们的线程表中:

threadid  title
   46     Some Title

我需要知道的是,如果thread_ids或drc_settings与线程的threadid匹配,如何从线程获取标题?

这就是我所处的位置,但我对这个问题的看法有些偏差。

$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>';

如果我从foreach中删除查询,我可以获得$ res返回1 46 23,这让我觉得查询无法正常工作。

最终结果将是输入IE中指定的每个线程的链接列表:

  • 一些标题
  • 另一个线程
  • 主题标题

这是我当前设置返回的错误

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;

使用$ 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};"

然后根据需要循环遍历结果集以构建$ post数组。

暂无
暂无

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

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