PHP加入多个表不能按预期工作

时间:2016-04-04 10:10:57

标签: php join left-join inner-join

我将简要解释一下到目前为止我所做的事情 - 首先,我有多个商店,每个商店都有自己的表,其中包含 id,item,qty和price ,还有一个名为all_items的主表,其中包含总数量取自每个商店表。

现在每个商店表都有相同的商品编号。在这里你可以看到表格的样子:

存储表名为:S1,S2,S3

S1

id item qty price
1  x1   10  12
2  x2   10  15
3  x3   5   5

S2

id item qty price
1  x1   10  12
2  x4   6   6

S3

id item qty price
1  x3   1   5
2  x6   5   5
3  x7   5   12

all_items - 包含原始总数量

id item qty price
1  x1   20  12
2  x2   10  15
3  x3   6   5
4  x4   6   6
5  x6   5   5
6  x7   5   12

现在好了,我已经决定找出所有商店中每件商品的可用数量,以便进行比较 - 如下:

item price S1  S2  S3
x1   12    10  10  -
x2   15    10  -   -
x3   5     5   -   5
x4   6     -   6   -
x6   5     -   -   5
x7   12    -   -   5

我希望现在对你说清楚。我创建了一个带有复选框输入类型的表单(对于每个商店),以允许用户选择他想要比较的商店,因此在提交表单后,PHP页面包含以下代码:

$allstore = $_POST['store']; //Collects name from checkbox ticks under form


 function createSelect($allstore)
{
    if (empty($allstore))
        return "";

    $querySelect = "";
    $queryJoin = "";
    $baseTable = "";
    foreach ($allstore as $store => $value) {
        if (!$querySelect) {
            $baseTable = "all_items";
            $querySelect = "SELECT " . $store . ".item_no, " . $store . ".actual_price, " . $store . ".selling_price, " . $store . ".qty as " . $store;
        } else {
            $querySelect .= ", " . $store . ".qty as " . $store;
            $queryJoin .= "
            INNER JOIN " . $store . " ON " . $baseTable . ".item_no = " . $store . ".item_no";
        }
    }
    $querySelect .= " FROM " . $baseTable;
    $query = $querySelect . $queryJoin;

    return $query;
}


$allstore = array(); // The below code allows function to know how many stores selected in $allstore 
if (!empty($_POST['store'])) {
    foreach ($_POST['store'] as $value) {
         $allstore["s_".$value] = 0; // or 1, it doesn't matter because your function adds all the keys
    }
}

var_dump(createSelect($allstore)); // Output SQL 

$query = (createSelect($allstore)); 
$result = mysql_query($query);
//Rest of the code .....

现在,如果您发现$baseTable = "all_items";使整个查询失败。但是,如果我将其值更改为$baseTable = $store;它会起作用并显示输出但不是预期的,因为事实证明S1现在是主要的,结果将完全不同,因为它仅在S1项目上进行中继。

如果您能提供任何有用的解决方案,将不胜感激。

1 个答案:

答案 0 :(得分:0)

为什么不使用UNION

function createSelect($stores)
{
    $query = "";
    $baseTable = "all_items";
    foreach($stores as $i => $store)
    {
        $query .= "(SELECT {$store}.id AS {$store}_id, {$store}.item AS {$store}_item, {$store}.price AS {$store}_price, {$store}.qty AS {$store}_qty, '{$store}' as Source FROM {$store}) UNION ";
    }
    $query .= "(SELECT all_items.id AS {$baseTable}_id, {$baseTable}.item AS {$baseTable}_item, {$baseTable}.price AS {$baseTable}_price, {$baseTable}.qty AS {$baseTable}_qty, '{$baseTable}' as Source FROM {$baseTable})";
    return $query; 
}

$result = mysql_query(createSelect($allStore));
//Rest of code
//if my tables are S1, S1, and my $baseTable is bs
//echo createSelect(array('S1', 'S2'); will output (SELECT S1.id AS S1_id, S1.item AS S1_item, S1.price AS S1_price, S1.qty AS S1_qty, 'S1' as Source FROM S1) UNION (SELECT S2.id AS S2_id, S2.item AS S2_item, S2.price AS S2_price, S2.qty AS S2_qty, 'S2' as Source FROM S2) UNION (SELECT all_items.id AS all_items_id, all_items.item AS all_items_item, all_items.price AS all_items_price, all_items.qty AS all_items_qty, 'all_items' as Source FROM all_items)