2016-05-08 2 views
1

enter image description hereここに私のPHP上でこのsqlコードを使用しています。しかし、すべての製品について、私はそれらの2倍またはそれらの複製を取得しています。私はそれが私が得る製品を複製しないで、一度だけ得るようにするためにどのように書くべきですか?私の内部結合テーブルから重複した値を取得しています

<?php 
include ('classes/functions.php'); 

if(isset($_POST['user_id'])){ 
$user_id = $_POST['user_id']; 
$check_receipt = "select si.shipping_name, 
    si.shipping_address, 
    si.shipping_contact, 
    si.shipping_email, 
    o.order_date, 
    o.trx_id, 
    o.tracking_num, 
    o.quantity, 
    o.store_id, 
    o.product_id, 
    p.product_title, 
    p.product_price, 
    p.product_img1, 
    p.product_weight 
from shipping_infos si 
inner join orders o 
on si.user_id = o.user_id 
inner join products p 
on p.product_id = o.product_id 
where si.user_id='".$user_id."' order by o.trx_id;" ; 

     $run_receipt_checking = mysqli_query($con, $check_receipt); 
     $result = array(); 
    while($row = mysqli_fetch_array($run_receipt_checking)){ 
    array_push($result, 
    array(
      'shipping_name'=>$row[0], 
      'shipping_address'=>$row[1], 
      'shipping_contact'=>$row[2], 
      'shipping_email'=>$row[3], 
      'order_date'=>$row[4], 
      'trx_id'=>$row[5], 
      'tracking_num'=>$row[6], 
      'quantity'=>$row[7], 
      'store_id'=>$row[8], 
      'product_id'=>$row[9], 
      'product_title'=>$row[10],    
      'product_price'=>$row[11], 
      'product_img1'=>$row[12],  
      'product_weight'=>$row[13]   

)); 
} 
echo json_encode(array("result"=>$result)); 
} 

?>

+0

私はそれがPHPではSQLではない問題だと思います。 PHPでクエリをどのように実行するかについて、より多くのコードを投稿できますか? –

+0

'select'の直後に' distinct'を追加 –

+0

データベース自体に重複したエントリがあるかどうか尋ねると良いでしょうか? –

答えて

0

私はあなたのp.product_idがあなたのDB内で一意のフィールドです推測しています。

もしそうなら、ステートメントの最後にGROUP BY p.product_idを追加することができます。

$check_receipt = "select si.shipping_name, 
    si.shipping_address, 
    si.shipping_contact, 
    si.shipping_email, 
    o.order_date, 
    o.trx_id, 
    o.tracking_num, 
    o.quantity, 
    o.store_id, 
    o.product_id, 
    p.product_title, 
    p.product_price, 
    p.product_img1, 
    p.product_weight 
    from shipping_infos si 
    inner join orders o 
on si.user_id = o.user_id 
inner join products p 
on p.product_id = o.product_id 
where si.user_id='".$user_id."' order by o.trx_id group by p.product_id;" ; 
2

これはコメントには長すぎます。

結果セット内の重複は、結合で複数の一致を意味します。サンプルデータがなければ、どこで発生しているのかを知ることは本当に難しいです。

クエリに基づいて推測しなければならない場合は、ユーザーがshipping_infosに複数の行を持つ可能性があります。間違いなく他の可能性がありますが、私は単純なクエリから始まり、最終クエリまで構築します。

+0

も正しく、サンプルデータは必須です! – Ayub

関連する問題