如何组合两个Select查询';在同一个php中输出到一个JsonObject?
我的php中有两个select查询如何组合两个Select查询';在同一个php中输出到一个JsonObject?,php,android,mysql,json,Php,Android,Mysql,Json,我的php中有两个select查询 $rr=mysql_query("SELECT * FROM tbl_sales_target WHERE rep = '502' AND DATE_FORMAT(date, '%Y-%m') = '2016-03'"); $r=mysql_query("SELECT SUM(tbl_sales.ton_ach/1000) AS tonSum, SUM(tbl_sales.val_ach/1000000) AS valSum FROM tbl_sales I
$rr=mysql_query("SELECT * FROM tbl_sales_target WHERE rep = '502' AND DATE_FORMAT(date, '%Y-%m') = '2016-03'");
$r=mysql_query("SELECT SUM(tbl_sales.ton_ach/1000) AS tonSum, SUM(tbl_sales.val_ach/1000000) AS valSum FROM tbl_sales INNER JOIN tbl_mas_customer on tbl_sales.customer = tbl_mas_customer.customer WHERE rep = '502' AND DATE_FORMAT(date, '%Y-%m') = '2016-03'");
我想将每个查询输出合并到一个Json中。
这是两个查询的Json输出
{"feed":[{"rep":"502","date":"2016-03-01","val_tar":"64.91","ton_tar":"84.59","col_tar":"69.92"}]}{"feed":[{"tonSum":"45.201940","valSum":"33.150250"}]}
我想要的是这样的东西。
{"feed":[{"rep":"502","date":"2016-03-01","val_tar":"64.91","ton_tar":"84.59","col_tar":"69.92","tonSum":"45.201940","valSum":"33.150250"}]}
<?php
error_reporting(-1);
require_once 'include/Config.php';
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("connection failed");
mysqli_select_db($con,DB_DATABASE) or die("db selection failed");
$r=mysqli_query($con,"SELECT tbl_sales_target.*,
SUM(tbl_sales.ton_ach/1000) AS tonSum,
SUM(tbl_sales.val_ach/1000000) AS valSum
FROM tbl_sales_target
JOIN tbl_mas_customer ON tbl_mas_customer.rep = tbl_sales_target.rep
JOIN tbl_sales ON tbl_sales.customer = tbl_mas_customer.customer
WHERE tbl_sales_target.rep = '502'
AND DATE_FORMAT(date, '%Y-%m') = '2016-03'");
$result = array();
while($row=mysqli_fetch_array($r)){
array_push($result,array('rep'=>$row[0],'date'=>$row[1],'val_tar'=>$row[2],'ton_tar'=>$row[3],'col_tar'=>$row[4],'tonSum'=>$row[5],'valSum'=>$row[6]));}
$json['feed']= $result;
echo json_encode($json);
mysqli_close($con);
?>
这是我的php脚本
$r=mysql_query("select sum(tbl_sales.ton_ach/1000) as tonSum, sum(tbl_sales.val_ach/1000000) as valSum from tbl_sales inner join tbl_mas_customer on tbl_sales.customer = tbl_mas_customer.customer where rep = '502' and DATE_FORMAT(date, '%Y-%m') = '2016-03'");
$rr=mysql_query("select * from tbl_sales_target where rep = '502' and DATE_FORMAT(date, '%Y-%m') = '2016-03'");
$result = array();
$resultt = array();
while($row=mysql_fetch_array($rr)){
array_push($result,
array('rep'=>$row[0], 'date'=>$row[1],'val_tar'=>$row[2],'ton_tar'=>$row[3],'col_tar'=>$row[4]));}
while($row=mysql_fetch_array($r)){
array_push($resultt,
array('tonSum'=>$row[0],
'valSum'=>$row[1]));}
$json['feed']= $result;
echo json_encode($json);
$jsonn['feed']= $resultt;
echo json_encode($jsonn);
我的桌子
已编辑
$r=mysql_query("SELECT tbl_sales_target.*,
SUM(tbl_sales.ton_ach/1000) AS tonSum,
SUM(tbl_sales.val_ach/1000000) AS valSum
FROM tbl_sales_target
JOIN tbl_mas_customer ON tbl_mas_customer.rep = tbl_sales_target.rep
JOIN tbl_sales ON tbl_sales.customer = tbl_mas_customer.customer
WHERE tbl_sales_target.rep = '502'
AND DATE_FORMAT(date, '%Y-%m') = '2016-03'");
$result = array();
while($row=mysql_fetch_array($r)){
array_push($result,
array('rep'=>$row[0],
'date'=>$row[1],'val_tar'=>$row[2],'ton_tar'=>$row[3],'col_tar'=>$row[4],'tonSum'=>$row[5],'valSum'=>$row[6]));}
$json['feed']= $result;
echo json_encode($json);
enter code here
private void getJsonRequest(final Button a, final Button b, final Button c) {
DateFormat df = new SimpleDateFormat("yyyy-MM");
final String systemDate = df.format(new Date());
final SQLiteHandler sqLiteHandler = new SQLiteHandler(getApplicationContext());
Cursor cr = sqLiteHandler.getData(sqLiteHandler);
cr.moveToFirst();
do {
repNo = cr.getString(0);
} while (cr.moveToNext());
cr.close();
CustomJsonObjectRequest request = new CustomJsonObjectRequest(Request.Method.POST, AppConfig.URL_REP_SUMMERY, hashMap, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = new JSONObject(String.valueOf(response));
if (jsonObject.names().get(0).equals("feed")) {
productSummList = parseJsonResponse(response);
txtTarget.setText(productSummList.get(0).getTar_ton() + " MT");
double tar_ton = Double.parseDouble(productSummList.get(0).getTar_ton());
int intTar_ton = (int) tar_ton;
arcProgress.setMax(intTar_ton);
arcProgress.setFinishedStrokeColor(Color.GREEN);
arcProgress.setStrokeWidth(20f);
arcProgress.setBottomTextSize(35f);
arcProgress.setBottomText("Ton");
ObjectAnimator animation = ObjectAnimator.ofInt(arcProgress, "progress", 0, 30); // see this max value coming back here, we animale towards that value
animation.setDuration(1500); //in milliseconds
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtTarget.setText(productSummList.get(0).getTar_ton() + " MT");
arcProgress.setBottomText("Ton");
ObjectAnimator animation = ObjectAnimator.ofInt(arcProgress, "progress", 0, 30);
animation.setDuration(1500);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtTarget.setText(productSummList.get(0).getTar_col() + " Mil");
arcProgress.setBottomText("Collection");
}
});
c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtTarget.setText(productSummList.get(0).getTar_val() + " Mil");
arcProgress.setBottomText("Value");
}
});
//Toast.makeText(getApplicationContext(), "System Date " + systemDate, Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), "Database Date " + productSummList.get(0).getTar_ton(), Toast.LENGTH_SHORT).show();
//Log.d("Database Date ", productSummList.toString());
} else {
Toast.makeText(getApplicationContext(), "No Data Available", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
productSummList = parseJsonResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
hashMap = new HashMap<String, String>();
hashMap.put("rep_no", repNo);
hashMap.put("systemDate", systemDate);
return hashMap;
}
};
requestQueue.add(request);
}
private ArrayList<ProductSummery> parseJsonResponse(JSONObject response) {
ArrayList<ProductSummery> productSumList = new ArrayList<>();
if (response != null || response.length() > 0) {
try {
JSONArray arrayProduct = response.getJSONArray(KEY_FEED_NAME);
for (int i = 0; i < arrayProduct.length(); i++) {
JSONObject currentObject = arrayProduct.getJSONObject(i);
String repName = currentObject.getString(KEY_REP_ID);
String date = currentObject.getString(KEY_DATE);
String valTar = currentObject.getString(KEY_VALUE_TARGET);
String tonTar = currentObject.getString(KEY_TON_TARGET);
String colTar = currentObject.getString(KEY_COL_TARGET);
ProductSummery productSummery = new ProductSummery();
productSummery.setRepNo(repName);
productSummery.setDate(date);
productSummery.setTar_val(valTar);
productSummery.setTar_ton(tonTar);
productSummery.setTar_col(colTar);
productSumList.add(productSummery);
}
//Toast.makeText(getApplicationContext(), productSumList.toString(), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
return productSumList;
}
输出
{"feed":[{"rep":"502","date":"2016-03-01","val_tar":"64.91","ton_tar":"84.59","col_tar":"69.92"},{"tonSum":"45.201940","valSum":"33.150250"}]}
{"feed":[{"rep":"502","date":"2016-03-01","val_tar":"64.91","ton_tar":"84.59","col_tar":"69.92","tonSum":"45.201940","valSum":"33.150250"}]}
我想要什么
{"feed":[{"rep":"502","date":"2016-03-01","val_tar":"64.91","ton_tar":"84.59","col_tar":"69.92"},{"tonSum":"45.201940","valSum":"33.150250"}]}
{"feed":[{"rep":"502","date":"2016-03-01","val_tar":"64.91","ton_tar":"84.59","col_tar":"69.92","tonSum":"45.201940","valSum":"33.150250"}]}
安卓方法
$r=mysql_query("SELECT tbl_sales_target.*,
SUM(tbl_sales.ton_ach/1000) AS tonSum,
SUM(tbl_sales.val_ach/1000000) AS valSum
FROM tbl_sales_target
JOIN tbl_mas_customer ON tbl_mas_customer.rep = tbl_sales_target.rep
JOIN tbl_sales ON tbl_sales.customer = tbl_mas_customer.customer
WHERE tbl_sales_target.rep = '502'
AND DATE_FORMAT(date, '%Y-%m') = '2016-03'");
$result = array();
while($row=mysql_fetch_array($r)){
array_push($result,
array('rep'=>$row[0],
'date'=>$row[1],'val_tar'=>$row[2],'ton_tar'=>$row[3],'col_tar'=>$row[4],'tonSum'=>$row[5],'valSum'=>$row[6]));}
$json['feed']= $result;
echo json_encode($json);
enter code here
private void getJsonRequest(final Button a, final Button b, final Button c) {
DateFormat df = new SimpleDateFormat("yyyy-MM");
final String systemDate = df.format(new Date());
final SQLiteHandler sqLiteHandler = new SQLiteHandler(getApplicationContext());
Cursor cr = sqLiteHandler.getData(sqLiteHandler);
cr.moveToFirst();
do {
repNo = cr.getString(0);
} while (cr.moveToNext());
cr.close();
CustomJsonObjectRequest request = new CustomJsonObjectRequest(Request.Method.POST, AppConfig.URL_REP_SUMMERY, hashMap, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject jsonObject = new JSONObject(String.valueOf(response));
if (jsonObject.names().get(0).equals("feed")) {
productSummList = parseJsonResponse(response);
txtTarget.setText(productSummList.get(0).getTar_ton() + " MT");
double tar_ton = Double.parseDouble(productSummList.get(0).getTar_ton());
int intTar_ton = (int) tar_ton;
arcProgress.setMax(intTar_ton);
arcProgress.setFinishedStrokeColor(Color.GREEN);
arcProgress.setStrokeWidth(20f);
arcProgress.setBottomTextSize(35f);
arcProgress.setBottomText("Ton");
ObjectAnimator animation = ObjectAnimator.ofInt(arcProgress, "progress", 0, 30); // see this max value coming back here, we animale towards that value
animation.setDuration(1500); //in milliseconds
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtTarget.setText(productSummList.get(0).getTar_ton() + " MT");
arcProgress.setBottomText("Ton");
ObjectAnimator animation = ObjectAnimator.ofInt(arcProgress, "progress", 0, 30);
animation.setDuration(1500);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtTarget.setText(productSummList.get(0).getTar_col() + " Mil");
arcProgress.setBottomText("Collection");
}
});
c.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
txtTarget.setText(productSummList.get(0).getTar_val() + " Mil");
arcProgress.setBottomText("Value");
}
});
//Toast.makeText(getApplicationContext(), "System Date " + systemDate, Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), "Database Date " + productSummList.get(0).getTar_ton(), Toast.LENGTH_SHORT).show();
//Log.d("Database Date ", productSummList.toString());
} else {
Toast.makeText(getApplicationContext(), "No Data Available", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
productSummList = parseJsonResponse(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
protected Map<String, String> getParams() throws AuthFailureError {
hashMap = new HashMap<String, String>();
hashMap.put("rep_no", repNo);
hashMap.put("systemDate", systemDate);
return hashMap;
}
};
requestQueue.add(request);
}
private ArrayList<ProductSummery> parseJsonResponse(JSONObject response) {
ArrayList<ProductSummery> productSumList = new ArrayList<>();
if (response != null || response.length() > 0) {
try {
JSONArray arrayProduct = response.getJSONArray(KEY_FEED_NAME);
for (int i = 0; i < arrayProduct.length(); i++) {
JSONObject currentObject = arrayProduct.getJSONObject(i);
String repName = currentObject.getString(KEY_REP_ID);
String date = currentObject.getString(KEY_DATE);
String valTar = currentObject.getString(KEY_VALUE_TARGET);
String tonTar = currentObject.getString(KEY_TON_TARGET);
String colTar = currentObject.getString(KEY_COL_TARGET);
ProductSummery productSummery = new ProductSummery();
productSummery.setRepNo(repName);
productSummery.setDate(date);
productSummery.setTar_val(valTar);
productSummery.setTar_ton(tonTar);
productSummery.setTar_col(colTar);
productSumList.add(productSummery);
}
//Toast.makeText(getApplicationContext(), productSumList.toString(), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
return productSumList;
}
我想这应该能解决它
$feed = array("feed" => array_merge($result, $resultt));
echo json_encode($feed);
见:
另一种方法是简单地将2个查询合并为1个查询
SELECT tbl_sales_target.*,
SUM(tbl_sales.ton_ach/1000) AS tonSum,
SUM(tbl_sales.val_ach/1000000) AS valSum
FROM tbl_sales_target
JOIN tbl_mas_customer ON tbl_mas_customer.rep = tbl_sales_target.rep
JOIN tbl_sales ON tbl_sales.customer = tbl_mas_customer.customer
WHERE tbl_sales_target.rep = '502'
AND DATE_FORMAT(tbl_sales.date, '%Y-%m') = '2016-03'
AND DATE_FORMAT(tbl_sales_target.date, '%Y-%m') = '2016-03'
我想这应该能解决它
$feed = array("feed" => array_merge($result, $resultt));
echo json_encode($feed);
见:
另一种方法是简单地将2个查询合并为1个查询
SELECT tbl_sales_target.*,
SUM(tbl_sales.ton_ach/1000) AS tonSum,
SUM(tbl_sales.val_ach/1000000) AS valSum
FROM tbl_sales_target
JOIN tbl_mas_customer ON tbl_mas_customer.rep = tbl_sales_target.rep
JOIN tbl_sales ON tbl_sales.customer = tbl_mas_customer.customer
WHERE tbl_sales_target.rep = '502'
AND DATE_FORMAT(tbl_sales.date, '%Y-%m') = '2016-03'
AND DATE_FORMAT(tbl_sales_target.date, '%Y-%m') = '2016-03'
你能把样品寄出去吗。
tbl\u销售数据
和tbl\u主要客户数据
。您需要在第一个查询中使用联接,那么为什么不直接在第二个查询中选择列呢?而不是两个查询。它不起作用。我尝试对三个表使用内部联接。它不起作用。。[我的表格][1][1]:为什么表格与销售目标
没有关系?哪里是外键
和主键
关系?表\u sales\u目标没有主键。数值重复。你能把样品贴出来吗。tbl\u销售数据
和tbl\u主要客户数据
。您需要在第一个查询中使用联接,那么为什么不直接在第二个查询中选择列呢?而不是两个查询。它不起作用。我尝试对三个表使用内部联接。它不起作用。。[我的表格][1][1]:为什么表格与销售目标
没有关系?哪里是外键
和主键
关系?表\u sales\u目标没有主键。值正在复制它不工作。。。这个错误来了。。。警告:mysql_fetch_array()希望参数1是resource,布尔值在第31行{“feed”:[]}上的C:\xampp\htdocs\ceat_sap\rep_summery_test.php中给出。对于第一个答案,它实际上给出了一个输出,但不是我提到的一个json。它给了我类似于{“feed”:[{“rep”:“502”,“date”:“2016-03-01”,“val_tar”:“64.91”,“tonu tar”:“84.59”,“col_tar”:“69.92”},{“tonSum”:“45.201940”,“valSum”:“33.150250”}我不需要},{在两个jsonNog之间,确定你对查询做了什么,它应该工作。同样对于第一个答案,我也非常确定它应该工作。你确定你回显了json_encode($feed)吗?也许可以添加代码,这样我就可以看到你做了什么。我更新了它……第一个是工作的。但是我想要的json没有},{在“69.92”和“tonSum”之间…我理解,但数组\ u merge函数不可能在它们之间留下逗号。您可以将代码发送到使用第一个解决方案的位置吗?我不完全确定第二个解决方案出了什么问题它不起作用…此错误出现…警告:mysql\ u fetch\ u array()期望参数1是resource,在第31行的C:\xampp\htdocs\ceat_sap\rep_summery_test.php中给出布尔值{“feed”:[]}对于第一个答案,它实际上给出了一个输出,但不是我提到的一个json。它给了我类似以下内容{“feed”:[{“rep”:“502”,“date”:“2016-03-01”,“val_tar”:“64.91”,“ton_tar”:“84.59”,“col tar”:“69.92”}{“tonSum”:“45.201940”,“valSum”:“33.150250”}]}我不需要},{在两个jsonNog之间,确定您对查询做了什么,它应该可以工作。对于第一个答案,我非常确定它应该可以工作。您确定您回显了json_encode($feed)吗?也许可以添加代码,这样我就可以看到你做了什么。我更新了它…第一个已经工作了。但是我想要的json没有},{介于“69.92”和“tonSum”之间“…我理解,但是数组\合并函数不可能在它们之间留下逗号。您能将代码发送到使用第一个解决方案的位置吗?我不完全确定第二个解决方案出了什么问题