问题描述
||
我有两个这样的表:
table {node}
`nid`,`uid`,`type`
1 1 basketball
2 1 basketball
3 1 football
4 2 football
5 2 basketball
table {strato_ticket}
`tid`,`author_uid`,`purpose`,`active`
1 1 \'Ticket to a basketball game\' TRUE
2 1 \'Ticket to a football game\' TRUE
3 2 \'Ticket to a football game\' FALSE
我想生成一个报告,该报告计算每种类型的节点的数量,然后计算每个用户与该类型的节点相关联的活动票证的数量。
我的解决方案使用了sql和PHP的组合:对于我感兴趣的每种节点,我都有一个PHP循环,从而简化了SQL查询,并将其从“类型”转换为“用途”,例如
$node_types = array(\'basketball\',\'football\');
foreach($node_types as $node){
switch($type){
case \'basketball\':
$purpose = array(\'Ticket to a basketball node\');
break;
case \'football\':
$purpose = array(\'Ticket to a football game\');
break;
}
$where = \" WHERE ({strato_ticket}.`purpose` = \'\"
.implode(\"\' OR {strato_ticket}.`purpose` = \'\",$purpose).\"\')\";
最后我有麻烦的地方,SQL查询。当我只计算每个用户拥有的节点时,它运行良好:
$query = \"
SELECT uid,count( * ) AS nodes_owned
FROM {node} WHERE `type` = \'$type\'
GROUP BY uid ORDER BY nodes_owned DESC
\";
$query = db_query($query);
output:
Now displaying info for basketball.
uid nodes_owned
1 2
2 1
Now displaying info for football.
uid nodes_owned
1 1
2 1
但是,现在我需要查询另一个表strato_ticket,事情变得复杂了,我的查询返回FALSE而不抛出错误(我认为)。
$query = \"
SELECT count(*) as tickets
FROM {strato_ticket} INNER JOIN (
SELECT node.uid,count( * ) AS nodes_owned
FROM {node} WHERE `type` = \'$type\'
GROUP BY uid
) AS {nodecount}
ON {strato_ticket}.`author_uid` = {nodecount}.`uid`
$where
GROUP BY nodecount.uid ORDER BY nodecount.nodes_owned DESC
\";
$query = db_query($query);
我对sql不太满意,也不太确定它的中断方式。可以使用一点帮助吗?
理想地想看
uid nodes_owned tickets
//basketball
1 2 1
2 1 0
//football
1 1 1
2 1 0
解决方法
除了占位符(我可以稍后再讲)之外,我认为这可以解决问题。
$form = array();
$node_types = array(\'basketball\',\'football\');
// if($user->uid == 1){
$form[$type][] = array(\'#value\'=>\"Showing how many of each node type each user owns.\".\'<br/>\');
foreach($node_types as $type){
// Count the number of nodes each user owns of $type.
$form[$type][] = array(\'#value\'=>\"Now displaying info for $type\".\'s. <br/>\');
switch($type){
case \'basketball\':
$purpose = array(\'ticket to a basketball game\',\'basketball\');
break;
case \'football\':
$purpose = array(\'ticket to a football game\');
break;
}
$purpose = implode(\"\',\'\",$purpose);
//@todo : Make a temporary table to query against so I\'m not hitting node table multiple times.
$ticketquery = \"
SELECT author_uid,purpose,COUNT( * ) AS invitees_accepted
FROM {strato_ticket}
WHERE purpose IN (\'$purpose\')
GROUP BY author_uid,`purpose`
\";
$nodequery = \"
SELECT node.uid,count( * ) AS nodes_owned,type
FROM {node}
WHERE `type` IN (\'$type\')
GROUP BY uid,type\";
$query = \"
SELECT * FROM
($nodequery) AS nt
JOIN
($ticketquery) AS tt
ON nt.uid = tt.author_uid
GROUP BY nt.uid ORDER BY nt.nodes_owned DESC
\";
drupal_set_message(\'Query is <br/>\'.$query);
//return;
$query = db_query($query);
$first = true;
while ($rec = db_fetch_object($query)){
if($first){
$form[$type][] = array(\'#value\'=>\"And the winner is: \".print_r($rec,true).\'<br/>\');
$first = false;
}
else {
$form[$type][] = array(\'#value\'=>print_r($rec,true).\'<br/>\');
}
}
// }
}