我有来自mysql数据库的3个表,我需要将它们组合起来以获得一个JSON输出...
第一个表很容易获得JSON输出所以我写道:
try {
$result = $db->prepare('SELECT datum,vrsta_prodaje,artikl,bruto_kol,jmere,skladiste FROM prodaja WHERE user_id=:user_id');
$result->bindParam(':user_id', $user_id);
$result->execute();
foreach($result as $r) {
$temp = array();
$temp['datum'] = (string) $r['datum'];
$temp['vrsta'] = (string) $r['vrsta_prodaje'];
$temp['artikl'] = (string) $r['artikl'];
$temp['6'] = (int) $r['bruto_kol'].' '.$r['jmere'];
$temp['jmere'] = (string) $r['jmere'];
$temp['skladiste'] = (string) $r['skladiste'];
//here I will add empty json becouse on second table I have one more column data
$temp['id_parcele'] = (string) '';
$rows[] = $temp;
}
$table['data'] = $rows;
$jsonTable = json_encode($table);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo $jsonTable;
但我需要在这里再合并两个表track_djub
和track_berba
我必须得到:
SELECT datum,vrsta,stavka,kolicina,jmere,skladiste,id_parcele FROM track_djub WHERE user_id=:user_id and
来自track_berba我必须得到:
SELECT datum,stavka,prinos,jmere,skladiste,id_parcele FROM track_berba WHERE user_id=:user_id
毕竟我需要按datum
(日期)
请告诉我如何将这3个表合并为DATUM订购的一个JSON输出
我怎么能这样做?
更新:我尝试做什么:但是不能获取任何数据:
try {
$result = $db->prepare('SELECT datum,vrsta_prodaje,artikl,bruto_kol,jmere,skladiste FROM prodaja WHERE user_id=:user_id');
$result->bindParam(':user_id', $user_id);
$result->execute();
foreach($result as $r) {
$temp = array();
$temp['datum'] = (string) $r['datum'];
$temp['vrsta'] = (string) $r['vrsta_prodaje'];
$temp['artikl'] = (string) $r['artikl'];
$temp['6'] = (int) $r['bruto_kol'].' '.$r['jmere'];
$temp['jmere'] = (string) $r['jmere'];
$temp['skladiste'] = (string) $r['skladiste'];
$temp['id_parcele'] = '';
$rows[] = $temp;
}
$result1 = $db->prepare('SELECT datum,vrsta,stavka,kolicina,jmere,id_parcele FROM track_djub WHERE user_id=:user_id');
$result1->bindParam(':user_id', $user_id);
$result1->execute();
foreach($result1 as $a) {
$temp1 = array();
$temp1['datum'] = (string) $a['datum'];
$temp1['vrsta'] = (string) $a['vrsta'];
$temp1['artikl'] = (string) $a['stavka'];
$temp1['6'] = (int) $a['kolicina'].' '.$a['jmere'];
$temp1['jmere'] = (string) $a['jmere'];
$temp1['skladiste'] = (string) '';
$temp1['id_parcele'] = (int) $a['id_parcele'];
$rows1[] = $temp1;
}
$result2 = $db->prepare('SELECT datum,kultura,prinos,jmere,id_parcele FROM track_berba WHERE user_id=:user_id');
$result2->bindParam(':user_id', $user_id);
$result2->execute();
foreach($result2 as $b) {
$temp2 = array();
$temp2['datum'] = (string) $b['datum'];
$temp2['vrsta'] = (string) 'Kultura';
$temp2['artikl'] = (string) $b['kultura'];
$temp2['6'] = (int) $b['prinos'].' '.$b['jmere'];
$temp2['jmere'] = (string) $b['jmere'];
$temp2['skladiste'] = (string) '';
$temp2['id_parcele'] = (int) $b['id_parcele'];
$rows2[] = $temp2;
}
$table['data'] = $rows.$rows1.$rows2;
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
echo $jsonTable;