我想在两个表之间建立连接

时间:2015-04-15 10:46:00

标签: php mysql sql database join

我有两个表,一个是变量而另一个是固定的。

$ tab 结构

id  int(11)  autoincrement
cod     text         
den_material    text         
furnizor    varchar(255)         
cant_reper  varchar(255)         
lg  varchar(255)    No       
cod_furnizor    varchar(255)         
obs     tinytext         
data    date    

nomenclator 表结构

nid      int(11)    autoincrement    
sap      text            
denumire     text            
grupa    varchar(255)            
unitate 

我想加入他们,在 $ tab 中生成一个具有以下结构的单个表

id  int(11)  autoincrement
cod     text        --> to get datas from nomenclator table sap as column 
den_material    text    -> to get datas from nomenclator table denumire as column    
furnizor    varchar(255)         
cant_reper  varchar(255)         
lg  varchar(255)    No       
cod_furnizor    varchar(255)         
obs     tinytext         
data    date

完整的源代码

 session_start();

// preserve selection for ajax call
if (!empty($_POST["tables"]))
{
    $_SESSION["tab"] = $_POST["tables"];
    $tab = $_SESSION["tab"];
}

// update on ajax call
if (!empty($_GET["grid_id"]))
    $tab = $_SESSION["tab"];

if (!empty($tab))
{
    $g = new jqgrid();

    // set few params
    $grid["caption"] = "Comanda : '$tab'";
    $grid["autowidth"] = true;
    $grid["multiselect"] = false; // allow you to multi-select through checkboxes
$grid["form"]["position"] = "center";
$grid["view_options"] = array("width"=>"500");



    $g->select_command = "SELECT t.* FROM `$tab` AS t, `nomenclator` AS n
WHERE n.`sap` = t.`cod` OR n.`denumire` = t.`den_material`;";



    $g->set_options($grid);

    $g->set_actions(array(  
                        "add"=>true, // allow/disallow add
                        "edit"=>true, // allow/disallow edit
                        "delete"=>true, // allow/disallow delete
                        "view"=>true, // allow/disallow delete
                        "rowactions"=>true, // show/hide row wise edit/del/save option
                        "search" => "advance", // show single/multi field search condition (e.g. simple or advance)
                        "showhidecolumns" => false
                    ) 
                );




    // set database table for CRUD operations

    $g->table = $tab;


    $col = array();
$col["title"] = "sap";
$col["name"] = "sap"; 
$col["width"] = "10";
$col["editable"] = true;
$col["hidden"] = true;
$cols[] = $col;  



$col = array(); 
    $col["index"] = "furnizor";
    $col["title"] = "Comerciale asigurate de: ";
    $col["editable"] = true;
    $col["edittype"] = "select"; // render as checkbo
$col["editoptions"] = array("value"=>'Ramira:Ramira;Beneficiar:Beneficiar', "multiple" => true);
    $cols[] = $col;

        $col = array();
$col["index"] = "den_material";
$col["title"] = "Denumire Material";
$col["name"] = "denumire"; 
$col["editable"] = true;
$col["width"] = "80";
$col["searchoptions"] = array("value" => $str, "separator" => ":", "delimiter" => ";");
$col["search"] = true;
$col["formatter"] = "autocomplete"; // autocomplete
$col["formatoptions"] = array(  "sql"=>"SELECT t.* 
FROM `$tab` AS t, `nomenclator` AS n
WHERE n.`sap` = t.`cod` OR n.`denumire` = t.`den_material`;",
                                "search_on"=>"name", 
                                "update_field" => "sap");
$cols[] = $col; 

1 个答案:

答案 0 :(得分:1)

下面的查询将获取行,其中$ tab中的数据与nomenclator中的数据相同。

SELECT t.* 
FROM `$tab` AS t, `nomenclator` AS n
WHERE n.`sap` = t.`cod` AND n.`denumire` = t.`den_material`;

下面的查询将获取行,其中$ tab中的任何数据与nomenclator中的相同。

SELECT t.* 
FROM `$tab` AS t, `nomenclator` AS n
WHERE n.`sap` = t.`cod` OR n.`denumire` = t.`den_material`;

尝试以上查询。如果您没有获得所需的结果集,请回复此处