2017-06-15 41 views
0

在我的代碼中,我有機會使用第一個SQL而不是第一個?
有人可以幫我嗎?我有什麼機會在我的代碼中使用第一個SQL,而不是第一個?

 //return supplies 
      SELECT * FROM suppliers WHERE id=$id 

     //return all suppliers itens 
    SELECT 
    suppliers.*, 
    third_party_services.name as servicename 
FROM 
    suppliers 
INNER JOIN 
    suppliers_services 
ON 
    suppliers.id = suppliers_services.supplier_id 
LEFT JOIN 
    third_party_services 
ON 
    suppliers_services.id = third_party_services.id 
WHERE 
    suppliers_services.supplier_id = $id 

我使用ajax的所有數據顯示成一個模式

阿賈克斯點這個.PHP

if(!empty($_POST)){ 
    if (isset($_POST['id']) && $_POST['id'] > 0){ 
     $id=$_POST['id']; 
     GetSuppliersView(); 
    } 
} 
function GetSuppliersView() { 
    global $db; 
    global $id; 
    try{ 
     $query = $db->query("SELECT * FROM suppliers WHERE id=$id"); 
     $row=$query->fetch(PDO::FETCH_ASSOC); 
     $result['success'] = true; 
     $result['result'] = $row; 
     echo json_encode($result); 
    return true; 
    } catch (PDOException $pe) { 
     return false; 
    } 
} 

,這是Ajax代碼

$('.itemview').click(function (e) { 
    e.preventDefault(); 
    $("#viewname").empty(); 
    $("#viewsuppliersocialname").val(""); 
    $("#viewsupplierfantasyname").val(""); 
    $("#viewsuppliercnpj").val(""); 
    $("#viewsuppliercontact").val(""); 
    $("#viewsupplieraddress").val(""); 
    $("#viewsupplierphone1").val(""); 
    $("#viewsupplierphone2").val(""); 
    $("#viewsupplierphone3").val(""); 
    $("#viewsupplieremail1").val(""); 
    $("#viewsupplieremail2").val(""); 
    $("#viewpaymentmethods").val(""); 
    var uid = $(this).data('id'); 
    $.ajax({ 
     type: "POST", 
     url: "resources/controllers/get.php", 
     data: 'id='+uid, 
     dataType: "json", 
     success: function (data) { 
      if (data.success) { 
       $('#viewname').append(data.result.name); 
       $('#viewsuppliersocialname').val(data.result.social_name); 
       $('#viewsupplierfantasyname').val(data.result.fantasy_name); 
       $('#viewsuppliercnpj').val(data.result.cnpj); 
       $('#viewsuppliercontact').val(data.result.contact); 
       $('#viewsupplieraddress').val(data.result.address); 
       $('#viewsupplierphone1').val(data.result.phone1); 
       $('#viewsupplierphone2').val(data.result.phone2); 
       $('#viewsupplierphone3').val(data.result.phone3); 
       $('#viewsupplieremail1').val(data.result.email1); 
       $('#viewsupplieremail2').val(data.result.email2); 
       $("#viewpaymentmethods").val(data.result.paymenttype).change(); 
       $("#MethodView").modal('show'); 
      } else { 

      } 
     } 
    }); 
}); 

有人可以幫我做這個嗎?

+0

有什麼用'JOIN'問題? – Ravi

回答

0
SELECT 
    suppliers.*, 
    third_party_services.name as servicename 
FROM 
    suppliers 
INNER JOIN 
    suppliers_services 
ON 
    suppliers.id = suppliers_services.supplier_id 
LEFT JOIN 
    third_party_services 
ON 
    suppliers_services.id = third_party_services.id 
WHERE 
    suppliers_services.supplier_id = $id 
0

你可以做多個聯接和它只是做出決議左至右:

SELECT 
    suppliers.*, 
    suppliers_services.*, 
    third_party_services.* 
FROM 
    suppliers 
INNER JOIN 
    suppliers_services 
ON 
    suppliers.id = suppliers_services.supplier_id 
LEFT JOIN 
    third_party_services 
ON 
    suppliers_services.id = third_party_services.id 
WHERE 
    supplier.id = $id 
+0

看一些角落案例行爲: 很明顯,即使帶有$ id的「third_party_services」不存在,也會產生一行,因爲您執行了左連接。這仍然涵蓋這種情況。如果$ id沒有在「supplier_services」中找到「supplier_id = $ id」中的某個內容,那麼您的舊查詢不會生成一行,並且它仍然不會因爲我們執行內部聯接(意思是它沒有「 t顯示在「suppliers_services」中,即使該供應商可能存在,它也不會產生一行)。 –

+0

它的工作原理與此:'選擇 供應商*, suppliers_services *, third_party_services * FROM 供應商 INNER JOIN suppliers_services ON suppliers.id = suppliers_services.supplier_id LEFT JOIN third_party_services ON suppliers_services。 .id = third_party_services.id WHERE suppliers_services.supplier_id = 154'但我該如何使用我的代碼使用它? – Debs

+0

我更新了主帖,我該如何使用它?我必須顯示所有供應商信息和所有服務 – Debs

相關問題