How do I put these two select together?

Asked

Viewed 32 times

0

How I put these two together 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 {

            }
        }
    });
});
  • Give an example if possible.

  • @Francisco ta lá

1 answer

1

Assuming suppliers.id and suppliers_services.supplier_id are the same id you can add one more LEFT JOIN and get 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 the LEFT JOIN.


Note: Use * in SELECT makes the search slower, whenever possible makes SELECT just the fields you need

Browser other questions tagged

You are not signed in. Login or sign up in order to post.