【问题标题】:How to create a dynamic query using php?如何使用 php 创建动态查询?
【发布时间】:2015-12-30 22:32:43
【问题描述】:

我有一张这样的桌子:

// mytable
+----+---------+------------+
| id | id_post | code_table |
+----+---------+------------+
| 1  | 34523   | 1          |
| 2  | 3453    | 4          |
| 3  | 43434   | 2          |
| 4  | 54321   | 1          |
| 5  | NULL    | NULL       |
| 6  | 32411   | 2          |
| 7  | 42313   | 1          |
| 8  | 34242   | 2          |
+----+---------+------------+
//                    ^ all of my focus is on this column

我也有这个数组:

$convert_code_name = array (
                        "1" => "Post1", 
                        "2" => "Post2", 
                        "3" => "Post3",
                        "4" => "Post4"  
                           );

现在我想创建这个

$query = "select * from post1
             union all
          select * from post2
             union all
          select * from post4";

          // there isn't "post3", because 3 isn't exist in the code_table column

我该怎么做?


这是我的尝试

// connect to database
$stm = $db->prepare('select * from mytable');
$stm->execute();
$result = $stm->fetch();

/* array_unique: removes duplicate values
   array_filter: removes "NULL" values */

array_filter(array_unique($result[code_table]), function($item) {
    return $item != 'NULL';
});

foreach($item as $numb){
    $query .= 'select * from'.$convert_code_name[$numb].'union all';
}

但我不知道为什么我的代码不起作用,我该怎么做?

【问题讨论】:

  • 我想你只是想要一个in 列表。
  • array_filter 返回一个新数组,你需要将它赋值给一个变量。
  • $result 只包含表格的第一行。你的意思是使用fetchAll
  • @Barmar 老实说我不知道​​,也许我需要fetchAll .. 我只是明白(到目前为止)最好使用DISTINCT(mysql 层)而不是@987654331 @(php层)

标签: php mysql arrays loops


【解决方案1】:

首先,在查询中使用SELECT DISTINCT 来获取唯一值,因此您不需要调用array_unique

然后,一旦获得所有值,就可以使用implode 将所有SELECT 查询与UNION ALL 连接起来。

$stm = $db->prepare("SELECT DISTINCT code_table FROM mytable WHERE code_table IS NOT NULL");
$stm->execute();
$results = $stm->fetchAll();
// This returns a 2-dimensional array, we just want one column
$results = array_column($results, 'code_table');

$query = implode(' UNION ALL ', array_map(function($code_table) use ($convert_code_name) {
    return "SELECT * FROM " . $convert_code_name[$code_table];
}, $results));

【讨论】:

  • 亲爱的主……!我不知道我该说什么.. 完美。 +1
  • 好吧,我不好意思再次 ping 你,但是......,看你的代码似乎完全正确。但我不知道为什么它不执行..!我的 php 版本很旧(5.2.6)。我认为array_map(function($code_table) 部分与我的php 版本有问题。有没有其他选择?
  • 您必须在旧版本中使用create_function。给读者练习,我没时间展示。
  • 它也没有array_column,它来自 PHP 5.5。阅读 shim 的文档。
  • 对此感到抱歉 -- 现在将 WHERE code_table IS NOT NULL 添加到查询中。
【解决方案2】:
$query .= 'select * from'.$convert_code_name[$numb].'union all';

会生成错误的sql,改成(我假设$convert_code_name[$numb]包含像Post1,Post2这样的全表名):

$query = '';
foreach($item as $numb){
    $query .= ($query!=''?' union all ':'') . 'select * from '.$convert_code_name[$numb];
}

【讨论】:

  • 您能告诉我如何在查询中选择NULL 值吗?
  • @stack where field is not null
【解决方案3】:

你只需要先执行这个查询:

SELECT DISTINCT code_table FROM events

然后,您可以根据结果构建第二个查询,每个现有 code_table 仅包含 1 行。

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 2013-03-25
    • 2017-01-22
    • 2014-10-04
    • 2012-02-09
    • 2018-10-27
    • 1970-01-01
    • 2013-09-16
    相关资源
    最近更新 更多