数据表 - 嵌套的SELECT&加入

时间:2016-11-02 20:50:19

标签: php mysql datatables

我正在使用datatables.net,但我正在努力弄清楚如何让嵌套查询和连接工作。我似乎无法找到任何例子。我可以用它来显示简单的查询(对于不同的表):

// DB table to use
$table = 'support_msgs';

// Table's primary key
$primaryKey = 'id';

// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
    array( 'db' => 'id', 'dt' => 0 ),
    array( 'db' => 'user_id',  'dt' => 1 ),
    array( 'db' => 'title',   'dt' => 2 ),
    array( 'db' => 'msg',   'dt' => 3 ),
    array( 'db' => 'date',   'dt' => 4 )
);

// SQL server connection information
$sql_details = array(
    'user' => 'user',
    'pass' => 'pass',
    'db'   => 'db',
    'host' => 'localhost'
);


require( 'ssp.class.php' );

// echo json_encode(
//     SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
// );
echo json_encode(
    SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns )
);

但是现在,我有这个问题,我看不到任何可以指向我的正确方向:

    SELECT
        vp.id, 
        vp.post_title,
        (
            SELECT 
                    vpm.meta_value 
            FROM 
                    vhyky_postmeta vpm 
            WHERE   vp.id = vpm.post_id
            AND     vpm.meta_key = 'cs_cause_goal_amount'
        ) AS Bonus,
        (
            SELECT 
                    vpm.meta_value
            FROM 
                    vhyky_postmeta vpm 
            WHERE   vp.id = vpm.post_id
            AND     vpm.meta_key = 'cs_cause_raised_amount'
        ) AS Deposit,
        wt.name,
        vp.post_content
FROM 
        vhyky_posts vp,
        vhyky_postmeta vpm,
        vhyky_term_relationships wtr,
        vhyky_term_taxonomy wtt,
        vhyky_terms wt
WHERE   vp.post_type = 'cs_cause'
AND     vp.post_status = 'publish'
AND     vp.id = vpm.post_id
AND     vp.id = wtr.object_id
AND     wtr.term_taxonomy_id = wtt.term_taxonomy_id
AND     wt.term_id = wtt.term_id
AND     wtt.term_id = '114'
group by vp.id

2 个答案:

答案 0 :(得分:1)

我会尝试以这种方式创建您的查询。由于我不知道您的数据库表是如何设置的,因此除了编辑一些语法之外,我无法帮助您。

SELECT 
    vp.id,
    vp.post_title,
    bonus.meta_value,
    deposit.meta_value,
    wt.name,
    vp.post_content
FROM
    vhyky_posts vp,
    vhyky_term_relationships wtr,
    vhyky_term_taxonomy wtt,
    vhyky_terms wt
LEFT JOIN
    (SELECT 
        vpm.post_id, vpm.meta_value
    FROM
        vhyky_postmeta vpm
    WHERE
        vpm.meta_key = 'cs_cause_goal_amount'
) AS bonus ON bonus.post_id = vp.id
LEFT JOIN
    (SELECT 
        vpm.post_id, vpm.meta_value
    FROM
        vhyky_postmeta vpm
    WHERE
        vpm.meta_key = 'cs_cause_raised_amount'
) AS deposit ON deposit.post_id = vp.id
WHERE
    vp.post_type = 'cs_cause'
        AND vp.post_status = 'publish'
        AND vp.id = wtr.object_id
        AND wtr.term_taxonomy_id = wtt.term_taxonomy_id
        AND wt.term_id = wtt.term_id
        AND wtt.term_id = '114'
GROUP BY vp.id

答案 1 :(得分:1)

我会创建一个类似于视图的表(如SQL视图,而不是MVC视图):在新表中组装所有需要的列。每当用户要求显示数据时,您可以将PHP脚本清空并填充表。然后,使用DataTables查询会更容易(特别是如果需要过滤器)。