How together did these two select?

0

How do I get these two select in a query?

//Retorna todos os dados do fornecedor
    SELECT * FROM suppliers WHERE id=$id

//Retorna todos os serviços do fornecedor
    SELECT suppliers_services.*, third_party_services.* FROM suppliers_services left JOIN third_party_services ON suppliers_services.id = third_party_services.id where suppliers_services.supplier_id = $id

I use an ajax to call .php and display in a modal .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 {

            }
        }
    });
});
    
asked by anonymous 15.06.2017 / 03:14

1 answer

1

Assuming that suppliers.id and suppliers_services.supplier_id are the same id you can add another LEFT JOIN and keep something like this:

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

So you get the 3 tables linked by the conditions of LEFT JOIN .

Note: Using * in SELECT makes the search slower, whenever possible it makes SELECT only of the fields you need

    
15.06.2017 / 06:31