在mySQL中动态选择表

时间:2016-08-09 06:38:38

标签: php mysql sql

我在mySQL中有一个查询

SELECT id FROM admin_products;

返回一个id列表,如此

+------+
| id   |
+------+
|    1 |
|    2 |
|    3 |
|    4 |
|    5 |
+------+

我正在使用PHP动态生成像

这样的表
vendor_1, vendor_2, vendor_3, vendor_4, vendor_5

现在我想编写一个查询来从表price中检索quantityid

例如

"ENTER QUERY HERE"

应检索

+-----------------------------+
| id   | price   | quantity   |
+-----------------------------+
|    1 |       23|          13| // price and quantity retrieved from table vendor_1 since id=1
|    2 |      158|          85| // price and quantity retrieved from table vendor_2 since id=2
|    3 |       15|           7| // price and quantity retrieved from table vendor_3 since id=3
|    4 |      112|           9| // price and quantity retrieved from table vendor_4 since id=4
|    5 |      123|         199| // price and quantity retrieved from table vendor_5 since id=5
+-----------------------------+

我现在用PHP做的是:

$conn = mysqli_connect($server,$user,$pwd,$db);
$sql = "SELECT id FROM admin_products";
$res = mysqli_query($conn,$sql);
if(mysqli_num_rows($res)>0){
    while($row = mysqli_fetch_assoc($res)){
        $product = array();
        $innerSQL = "SELECT price,quantity FROM vendor_".$row['id'];
        $innerRes = mysqli_query($conn,$innerSQL);
        if(mysqli_num_rows($innerRes)>0){
            while($innerRow = mysqli_fetch_assoc($innerRes)){
                 array_push($product,$row['id']);
                 array_push($product,$innerRow['price']);
                 array_push($product,$innerRow['quantity']);
            }
        }
   }
}

但它需要两次点击mySQL数据库。不能减少到一个吗?

修改

我后来意识到我的数据库结构不正确,动态创建表格是一个非常糟糕的主意,可能会在以后发生灾难

2 个答案:

答案 0 :(得分:1)

如果适合

,请尝试此操作
$table = "vendor"."_".$id; // this will create table name if $id = 1 then $table = vendor_1;
mysqli_query($connect , "SELECT * FROM $table");

<强>第二

如果您想同时获取所有data的{​​{1}},那么

1)从table抓取id并存储在admin_products

array

2)现在循环抛出$ids = array(1,2,3,4,5); 并创建array;

sql

答案 1 :(得分:1)

- 解决方案1:

注意:只有在您的vendor_x表id中有供应商ID才能匹配时,这才有效。 (正如Strawberry所说,动态生成表格是一个糟糕的主意。)

选择正确的ID后,您可以执行以下操作:

  • 连接到MySql Server

然后,您可以创建表名并将其存储在变量中。

$tableName = 'vendor_' . $id;

我建议在那之后检查表是否存在简单的查询:

$sql = "SHOW TABLES LIKE '$tableName'";

如果返回空结果,则可以抛出表不存在的异常,或者以任何方式处理它。

检查每个表后,为了确保它存在,您可以创建查询。

$joins = "";

$sql = "
    SELECT 
        v.id, 
        price, 
        quantity 
    FROM
        vendors AS v
";

foreach ($ids as $id) {
    $tableName = "vendor_" . $id;
    $tableAlias = "v".$id;

    $joins .= " LEFT JOIN " . $tableName . " AS ". $tableAlias ." 
                    ON (v.id = ". $tableAlias .".vendor_id) ";
}

$sql .= $joins;

然后执行查询。

- 解决方案2:

只创建一个表来管理您的供应商。它应该有这样的结构:

`id` // AI value
`vendor_id` // The id of the vendor to easily join it afterwards
`price`
`quantity`

您可以将其命名为vendor_product或任何

现在你只有一个简单的查询:

$sql = "
    SELECT
        v.id,
        vp.quantity,
        vp.price
    FROM
        vendors AS v
        LEFT JOIN vendor_product AS vp
            ON (vp.vendor_id = v.id)
";

编辑有关结构的评论:

供应商需要一张表,例如:

`vendor`:
    `id`, //AI value
    `username`,
    `password` // I suggest to you not to keep it in plain text.

`vendor_product` :
    `id`, //AI value
    `vendor_id`,
    `price`,
    `quantity`

我不知道你是否要存储有关每种产品的更多信息,但这应该可以解决问题。

如何以最低价格展示产品? 您需要通过某种方式匹配它们并按选择最低价格进行分组。