选择并列出子项和父项 - 替代项

时间:2012-10-11 18:16:27

标签: mysql sql

  

可能重复:
  SELECT and list children and parent

我从xception收到了这个答案:

https://stackoverflow.com/a/12770593/445820

不幸的是,GROUP_CONCAT对我来说不是解决方案而另一个问题太过分了,因此我再次提出这个问题。

我需要使用子查询替代该查询。由于我对查询不够好,这项任务也超出了我的能力范围。

请帮助。

以下是代码:

// Prepare query
$columns = "c.rule_id, c.rule_title, GROUP_CONCAT(r.rule_description ORDER BY r.rule_position ASC SEPARATOR '~sep~') AS rule_desc, ";
$columns .= "GROUP_CONCAT(r.parse_bbcode ORDER BY r.rule_position ASC SEPARATOR '~sep~') AS bbcode, ";
$columns .= "GROUP_CONCAT(r.parse_links ORDER BY r.rule_position ASC SEPARATOR '~sep~') AS links, ";
$columns .= "GROUP_CONCAT(r.parse_smilies ORDER BY r.rule_position ASC SEPARATOR '~sep~') AS smilies";

$sql_array = array(
    'SELECT'    => $columns,
    'FROM'      => array(RULES_TABLE => 'c'),
    'LEFT_JOIN' => array(
        array(
            'FROM'  => array(RULES_TABLE => 'r'),
            'ON'    => 'r.parent_id = c.rule_id',
        ),
    ),
    'WHERE'     => 'c.parent_id = 0 AND r.public = 1',
    'GROUP_BY'  => 'c.rule_id',
    'ORDER_BY'  => 'c.cat_position',
);

$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);

$cat_count      = 1;
$alpha_count    = 'abcdefghijklmnopqrstuvwxyz';

while ($row = $db->sql_fetchrow($result))
{
    $template->assign_block_vars('rules', array(
        'RULE_CATEGORY' => $row['rule_title'],
        'ROW_COUNT'     => $cat_count,
    ));

    $rules_ary      = explode('~sep~', $row['rule_desc']);
    $parse_bbcode   = explode('~sep~', $row['bbcode']);
    $parse_links    = explode('~sep~', $row['links']);
    $parse_smilies  = explode('~sep~', $row['smilies']);
    $counter        = 0;

    foreach ($rules_ary as $key => $rule)
    {
        $uid = $bitfield = $options = '';
        generate_text_for_storage($rule, $uid, $bitfield, $options, $parse_bbcode[$key], $parse_links[$key], $parse_smilies[$key]);

        $template->assign_block_vars('rules.rule', array(
            'RULE_DESC'     => generate_text_for_display($rule, $uid, $bitfield, $options),
            'ALPHA_COUNT'   => $alpha_count{$counter},
        )); 

        $counter++;
    }

    $cat_count++;
}
$db->sql_freeresult($result);

您可能不知道某些功能。仅供参考,这是与phpBB相关的代码。

很抱歉没有第一次添加代码。

1 个答案:

答案 0 :(得分:1)

// Prepare query
$columns = "c.rule_id, c.rule_title, r.rule_description AS rule_desc, ";
$columns .= "r.parse_bbcode AS bbcode, ";
$columns .= "r.parse_links AS links, ";
$columns .= "r.parse_smilies AS smilies";

$sql_array = array(
    'SELECT'    => $columns,
    'FROM'      => array(RULES_TABLE => 'c'),
    'LEFT_JOIN' => array(
        array(
            'FROM'  => array(RULES_TABLE => 'r'),
            'ON'    => 'r.parent_id = c.rule_id',
        ),
    ),
    'WHERE'     => 'c.parent_id = 0 AND r.public = 1',
    'ORDER_BY'  => 'c.cat_position',
);

$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);

$cat_count      = 0;
$alpha_count    = 'abcdefghijklmnopqrstuvwxyz';

$prev_rule_id   = 0;
$r_rule_titles  = array();

$rule_id = null;

while ($row = $db->sql_fetchrow($result))
{
    if( $rule_id != $row['rule_id'] ) {
            $rule_id = $row['rule_id'];
        $cat_count++;
        $counter = 0;
        $template->assign_block_vars('rules', array(
            'RULE_CATEGORY' => $row['rule_title'],
            'ROW_COUNT'     => $cat_count,
        ));
    }
    $uid = $bitfield = $options = '';
    generate_text_for_storage($row['rule_desc'], $uid, $bitfield, $options, $row['bbcode'], $row['links'], $row['smilies']);

    $template->assign_block_vars('rules.rule', array(
        'RULE_DESC'     => generate_text_for_display($row['rule_desc'], $uid, $bitfield, $options),
        'ALPHA_COUNT'   => $alpha_count{$counter},
    ));

    $counter++;    
}
$db->sql_freeresult($result);