2016-04-04 127 views
0

我会简单地解释我迄今为止所做的事情 - 首先,我有多个商店,每个商店都有自己的表格,其中包含id,item,qty和价格,并且有一个名为all_items的主表,其中包含从每个商店表中获取的总数量。PHP加入多个表格不能按预期工作

现在每个商店表都有相同的商品编号。在这里你可以看到表的样子: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项上进行中继。

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

回答

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) 
+0

感谢您的回复,根据你的代码,我发现了一个错误mysqli的'-mysql_fetch_assoc()预计参数1是资源,布尔given' –

+0

它似乎无望找到一个解决方案。 –

+0

我在我的最后复制了你的案例,它似乎运作良好。检查列名和表名是否与您已有的名称相对应。如果它不是,请让我知道 – KBJ