您好我有一个mysql查询,我希望按值按不同顺序获取结果。
这是代码。
$strQuery =
"SELECT
category.short_description AS category,
asset.asset_code AS code,
asset_model_number.short_description AS model,
asset_custom_field_helper.cfv_1 AS mnfserial,
asset_custom_field_helper.cfv_6 AS proctype,
asset_custom_field_helper.cfv_7 AS hdd,
asset_custom_field_helper.cfv_8 AS type,
asset_custom_field_helper.cfv_9 AS cond,
asset_custom_field_helper.cfv_10 AS drive,
asset_custom_field_helper.cfv_12 AS os,
asset_custom_field_helper.cfv_13 AS cpu,
asset_custom_field_helper.cfv_14 AS ram,
COUNT(*) AS total
FROM
asset_transaction
LEFT JOIN asset
ON asset_transaction.asset_id = asset.asset_id
LEFT JOIN asset_model
ON asset.asset_model_id = asset_model.asset_model_id
LEFT JOIN asset_model_number
ON asset.asset_model_number_id = asset_model_number.asset_model_number_id
LEFT JOIN asset_custom_field_helper
ON asset.asset_id = asset_custom_field_helper.asset_id
LEFT JOIN user_account
ON asset.created_by = user_account.user_account_id
LEFT JOIN category
ON asset_model.category_id = category.category_id
WHERE asset_transaction.transaction_id = ($txnid)
GROUP BY model";
$objDatabase = QApplication::$Database[1];
// Perform the Query
$objDbResult = $objDatabase->Query($strQuery);
本节我想按模型分组结果
echo "<h3>Total Model No<br></h3><strong>";
while ($mixRow = $objDbResult->FetchArray()) {
echo $mixRow['model']. " = " .$mixRow['total'];
echo "<br><strong>";
}
在下面的部分中,我希望按类型
显示结果组echo "<h3>Total Products<br></h3><strong>";
echo "<br>";
while ($row = $objDbResult->FetchArray()) {
echo $row['type']. " = " .$row['total'];
echo "<br><strong>";
我想避免每次编写sql查询,而是想使用此查询并在某处按值定义我的订单。我怎么能这样做?
答案 0 :(得分:1)
我希望这可以帮助你:)
function getQuery( $order_by_string )
{
return $query = 'Your query .... ORDER BY '.$order_by_string
}
$query = getQuery("something");
答案 1 :(得分:0)
您应该考虑创建一个查询构建器类,以便更自由地创建查询。
无论如何,只需设置组,您就可以创建函数getQueryGroupBy($query,$type)
,如下所示:
function getQueryGroupBy($query, $type) {
return $query . 'GROUP BY ' . $type;
}
您的查询将是:
$strQuery = "SELECT
category.short_description AS category,
asset.asset_code AS code,
asset_model_number.short_description AS model,
asset_custom_field_helper.cfv_1 AS mnfserial,
asset_custom_field_helper.cfv_6 AS proctype,
asset_custom_field_helper.cfv_7 AS hdd,
asset_custom_field_helper.cfv_8 AS type,
asset_custom_field_helper.cfv_9 AS cond,
asset_custom_field_helper.cfv_10 AS drive,
asset_custom_field_helper.cfv_12 AS os,
asset_custom_field_helper.cfv_13 AS cpu,
asset_custom_field_helper.cfv_14 AS ram,
COUNT(*) AS total
FROM
asset_transaction
LEFT JOIN asset
ON asset_transaction.asset_id = asset.asset_id
LEFT JOIN asset_model
ON asset.asset_model_id = asset_model.asset_model_id
LEFT JOIN asset_model_number
ON asset.asset_model_number_id = asset_model_number.asset_model_number_id
LEFT JOIN asset_custom_field_helper
ON asset.asset_id = asset_custom_field_helper.asset_id
LEFT JOIN user_account
ON asset.created_by = user_account.user_account_id
LEFT JOIN category
ON asset_model.category_id = category.category_id
WHERE
asset_transaction.transaction_id = ($txnid)";
然后您将调用此函数:
$query = $this->setGroupBy($strQuery, 'model');
或
$query = $this->setGroupBy($strQuery, 'type');
修改强>
所以你可能最终得到这个:
$baseQuery =
"SELECT
category.short_description AS category,
asset.asset_code AS code,
asset_model_number.short_description AS model,
asset_custom_field_helper.cfv_1 AS mnfserial,
asset_custom_field_helper.cfv_6 AS proctype,
asset_custom_field_helper.cfv_7 AS hdd,
asset_custom_field_helper.cfv_8 AS type,
asset_custom_field_helper.cfv_9 AS cond,
asset_custom_field_helper.cfv_10 AS drive,
asset_custom_field_helper.cfv_12 AS os,
asset_custom_field_helper.cfv_13 AS cpu,
asset_custom_field_helper.cfv_14 AS ram,
COUNT(*) AS total
FROM
asset_transaction
LEFT JOIN asset
ON asset_transaction.asset_id = asset.asset_id
LEFT JOIN asset_model
ON asset.asset_model_id = asset_model.asset_model_id
LEFT JOIN asset_model_number
ON asset.asset_model_number_id = asset_model_number.asset_model_number_id
LEFT JOIN asset_custom_field_helper
ON asset.asset_id = asset_custom_field_helper.asset_id
LEFT JOIN user_account
ON asset.created_by = user_account.user_account_id
LEFT JOIN category
ON asset_model.category_id = category.category_id
WHERE asset_transaction.transaction_id = ($txnid)";
$objDatabase = QApplication::$Database[1];
// Create the query
$modelQuery = getQueryGroupBy($baseQuery, 'model');
// Perform the Query
$objDbResultByModel = $objDatabase->Query($modelQuery);
echo "<h3>Total Model No<br></h3><strong>";
while ($mixRow = $objDbResultByModel->FetchArray()) {
echo $mixRow['model']. " = " .$mixRow['total'];
echo "<br><strong>";
}
// Create the query
$typeQuery = getQueryGroupBy($baseQuery, 'type');
// Perform the Query
$objDbResultByType = $objDatabase->Query($typeQuery);
echo "<h3>Total Products<br></h3><strong>";
echo "<br>";
while ($row = $objDbResultByType->FetchArray()) {
echo $row['type']. " = " .$row['total'];
echo "<br><strong>";
}
function getQueryGroupBy($query, $type) {
return $query . ' GROUP BY ' . $type;
}