好的,我有一个产品表。和一个通讯表。用户表和社交表。
社会
SID AUID BUID
1 1 2 // user 1 follow 2
2 1 3 // user 1 follow 3
3 2 1
我们可以看到用户ID 1跟随用户2和3
用户
UID NAME
1 me
2 imthenamefromuser2
3 imthenamefromuser3
产品
PID UID NAME TIMECREATE
1 2 user2product 12-04-2011
2 3 user3product 12-03-2011
评论 pcid =家长评论ID
CID UID PID COMMENT PCID
1 1 2 comment on product2 NULL
2 2 2 another comment on p2 NULL
3 3 2 someoneelse on p2 NULL
4 1 2 who are you? 3
5 3 2 im user 3 dude 4
6 1 1 a new post on p2 NULL
好的问题是
如何通过以下评论(最多20条评论)获取其关注者的产品列表?
继承人我到目前为止(没有评论)举例说明$ uid是1
function listFromFollower($uid){
#here
$data = $this->fetchAll("SELECT products.name AS product, users.name, products.pid, products.timecreate
FROM products
INNER JOIN users ON users.uid = products.uid
INNER JOIN social ON products.uid = social.buid
WHERE social.auid = :uid", array( 'uid' => $uid));
return $data;
}
它有点像
array
0 =>
array
'product' => string 'user2product' (length=9)
'name' => string 'imthenamefromuser2' (length=12)
'pid' => string '1' (length=1)
'timecreate' => string '2011-02-26 13:30:07' (length=19)
1 =>
array
'product' => string 'user3product' (length=8)
'name' => string 'imthenamefromuser3' (length=12)
'pid' => string '2' (length=1)
'timecreate' => string '2011-02-26 13:30:54' (length=19)
也许它应该像
array
0 =>
array
'product' => string 'user2product' (length=9)
'name' => string 'imthenamefromuser2' (length=12)
'pid' => string '1' (length=1)
'timecreate' => string '2011-02-26 13:30:07' (length=19)
0 =>
array
'name' => string ''
'comment' => string ''
1 =>
array
'name' => string ''
'comment' => string ''
0 =>
array
'name' => string 'a threaded comment'
'comment' => string ''
untill20 =>
array
'name' => string ''
'comment' => string ''
1 =>
array
'product' => string 'user3product' (length=8)
'name' => string 'imthenamefromuser3' (length=12)
'pid' => string '2' (length=1)
'timecreate' => string '2011-02-26 13:30:54' (length=19)
谢谢!
答案 0 :(得分:0)
您无法从查询中获得结果,就像您想要的那样。所以有两种选择。按原样运行查询并遍历每个产品以获取他们的评论。
或者得到一个包含所有注释的大结果,然后它们处理到数组以提取注释。
正如Kevin Peno评论的那样,你有什么理由需要这些数据吗?而不只是评论计数?因为这是一个简单的查询,没有处理。
无论如何,这里有一个如何使用LEFT JOIN执行第二个选项的示例:
function listFromFollower($uid){
$rows = $this->fetchAll("
SELECT products.name AS product, users.name, products.pid,
products.timecreate, commenters.name as commenter, comments.comment
FROM products
INNER JOIN users ON users.uid = products.uid
INNER JOIN social ON products.uid = social.buid
LEFT JOIN comments ON comments.pid = product.pid
INNER JOIN users AS commenters ON commenters.uid = comments.uid
WHERE social.auid = :uid", array( 'uid' => $uid));
$data = array();
foreach($rows as $row) {
// If we haven't added this product to data yet, add it.
if (!isset($data[$row['pid']])) {
$data[$row['pid']] = $row;
// Tidy up the data, we don't want commenter and comment here
unset($data[$row['pid']]['commenter']);
unset($data[$row['pid']]['comment']);
// Initialize an array for the comments
$data[$row['pid']]['comments'] = array();
}
$data[$row['pid']]['comments'][] = array(
'name' => $row['commenter'],
'comment' => $row['comment']
);
}
return $data;
}
现在你应该拥有和以前一样的数组,但现在数组中的每个元素都有一个数组,其中包含包含注释的关键注释。