0

这对我来说是一个复杂的查询,但如果有人知道请发布 id ...

我想做这样的东西

站点 1
发布
帖子


SITE 2
post
post
post

这是我的查询

$result = mysql_query("SELECT content.* FROM content WHERE content.site IN
          (SELECT site_id FROM site_subscription WHERE user_id = ".$_SESSION['userid'].")
          ORDER by content.site DESC");

while($row = mysql_fetch_assoc($result)) {
         $array[] = $row;
}

和即时显示帖子

if(is_array($array)) {  
    foreach($array as $row) {
        include($basepath.'/template/item.php');
    }
} 

这是有效的,帖子按site_id排序,但需要从其他表、表名站点获取site_name ,而不是按site_name的GROUP post,如上面的SITE 1、SITE 2、SITE 3

就像是 SELECT * FROM site WHERE id='content.site'

4

3 回答 3

0

"SELECT * FROM content INNER JOIN site ON content.site = site.id

在哪里 site.id 在 (

  SELECT site_id FROM site_subscription 

          WHERE user_id = ".$_SESSION['userid'].")

按网站名称订购”

于 2013-09-26T18:22:05.793 回答
0
$result = mysql_query("SELECT content.* FROM content
                       INNER JOIN site ON content.site = site.id
                       WHERE content.site IN
                       (SELECT site_id FROM site_subscription WHERE user_id = ".$_SESSION['userid'].")
                       ORDER by content.site DESC");
于 2013-09-26T18:22:25.993 回答
0

做一个JOIN代替子查询。尝试这样的事情:

SELECT content.*, site_name, FROM content 
JOIN site_subscription ON content.site = site_id
WHERE site_subscription.user_id = <USER_ID>
ORDER by content.site DESC
GROUP BY site_name

MysSQL 将处理这个查询,但它并不完全正确。您实际上无法选择content.*,因为在对行进行分组时,您必须只选择聚合字段。

于 2013-09-26T18:24:25.540 回答