php - mySQL如何合并语句

标签 php mysql

  1. 我有两个声明

a.

while (($row_1 = $STH_1 -> fetch_assoc()) !== null){
    $table_name = $row_1['table_name']; 
    $stmts_1[] = sprintf("                       
        SELECT *                         
        FROM $table_name                         
        WHERE date_time = $today_date ");
}

$stmt_1 = implode("\nUNION\n", $stmts_1);   
$stmt_1 .= "\nORDER BY date_time ASC";    
$STH_5_2 = $DBH_R->query($stmt_1);  
while (($row_5_2 = $STH_5_2 -> fetch_assoc()) !== null) {   }  

b.

$STH_e = $DBH_R->query("                         
    SELECT *                                                  
    FROM $table_name_2                                                  
    WHERE date_time = $today_date  ");   
while (($row_e = $STH_e -> fetch_assoc()) !== null) {   } 
  1. 查询 1 和查询 2 的源列不同
  2. 有一列相似 -> date_time,但 TIME(date_time) 不同,DATE(date_time) 将相同
  3. 我必须将所有内容合并到一个列表中(按时间(日期时间)ASC 排序)才能为股票脚本制作源代码

问:如何合并这两个语句?或者也许这两个查询?

//-------------------------------------------------------- ---------------- 更新-创建表语句

  1. 查询 1 的表 - 此类型接近 30 个表

    如果不存在则创建表v_c_ei_9001 (

    id int(11) 无符号 NOT NULL AUTO_INCRMENT,

    ei_code int(10) NOT NULL DEFAULT '0',

    date_time 日期时间 NOT NULL DEFAULT '0000-00-00 00:00:00',

    index_year varchar(10) NOT NULL DEFAULT 'ndnc',

    index_period varchar(10) NOT NULL DEFAULT 'nd',

    index_period_2 varchar(10) NOT NULL DEFAULT 'nd',

    index_unit varchar(10) NOT NULL DEFAULT 'nd',

    index_comment 文本不为空,

    主键(id), KEY 日期时间 (日期时间) ) ENGINE=MyISAM 默认字符集=utf8 AUTO_INCRMENT=608 ;

  2. 查询 2 的表 - 此类型只有一个

    如果不存在则创建表v_c_e (

    id int(11) 无符号 NOT NULL AUTO_INCRMENT,

    country_code int(10) NOT NULL DEFAULT '0',

    country_name varchar(50) NOT NULL DEFAULT 'ndnc',

    date_time 日期时间 NOT NULL DEFAULT '0000-00-00 00:00:00',

    event_title varchar(100) NOT NULL DEFAULT 'ndnc',

    event_released int(10) NOT NULL DEFAULT '0',

    event_comment varchar(500) NOT NULL DEFAULT 'ndnc',

    主键(id) ) ENGINE=MyISAM 默认字符集=utf8 AUTO_INCRMENT=168 ;

brgs

//-------------------------------------------------------- ------ 更新2

预查询代码 //从数据库中的所有表中,我们仅查询名称类似于 v_c_ei_9xxx 的表

$STH_1 = $DBH_R->query("SELECT table_name                        
                         FROM information_schema.tables                        
                        WHERE table_name                        
                         LIKE 'v_c_ei\_9%'
                         ");
$stmts_1 = array();

while (($row_1 = $STH_1 -> ..... // as above

最佳答案

  $stmts_1[] = sprintf("
SELECT * FROM (
   SELECT *                         
   FROM $table_name tn                     
   WHERE date_time = $today_date

UNION ALL

SELECT *                                                  
   FROM $table_name_2 tn2                                                  
   WHERE date_time = $today_date
)tmp;"                       

}

这应该做你想要的 - 我没有测试它,因为它取决于传递的变量,并且你没有提供 SQL 或代码。这是一个子选择查询。 UNION ALL 部分将两个查询的结果连接成一个结果。但是,两个表必须具有相同顺序的相同数量的列,否则这将引发类型不兼容或列数量错误的错误。

最好定义列和顺序,例如

  $stmts_1[] = sprintf("
SELECT * FROM (
SELECT col1, col2, col3, col4...... coln                          
FROM $table_name tn                     
WHERE date_time = $today_date

UNION ALL

SELECT col1, col2, col3, col4...... coln                                            
   FROM $table_name_2 tn2                                                 
   WHERE date_time = $today_date
)tmp;"

}

编辑:

应用我上面所说的相同逻辑,您可以处理一个表中的额外属性,但不能处理另一个表中的额外属性,如下所示:

SELECT * FROM(

SELECT
id as id,
ei_code as ei_code,
date_time as dt,
'' as country_name,
index_year as iy,
index_period as ip1,
index_period_2 as ip2,
index_unit as iu,
index_comment as ic,
'' as et,
'' as er,
'' as ec
  FROM `v_c_ei_9001` vce1

UNION ALL

SELECT
id as id,
country_code as country_code,
date_time as date_time,
country_name as country_name,
'' as iy,
'' as ip1,
'' as ip2,
'' as iu,
'' as ic,
event_title as et,
event_released as er,
event_comment as ec
FROM  `v_c_e` as vce2

)tmp

我根据您发布的创建表做出了一些假设,因为输入掩码非常相似。如果这些在您的上下文中是错误的,只需创建空白字段,就像我对 ip1、ip2 等所做的那样。我已经使用一些示例数据在 MySQL 中使用 CREATE 表测试了上述内容,效果很好。

编辑2:

$start2 = "SELECT
id as id,
country_code as country_code,
date_time as date_time,
country_name as country_name,
'' as iy,
'' as ip1,
'' as ip2,
'' as iu,
'' as ic,
event_title as et,
event_released as er,
event_comment as ec
FROM "; 

$count = 0;

$start = "SELECT
id as id,
ei_code as ei_code,
date_time as date_time,
'' as country_name,
index_year as iy,
index_period as ip1,
index_period_2 as ip2,
index_unit as iu,
index_comment as ic,
'' as et,
'' as er,
'' as ec
  FROM ";
//Loop through all table names using the variable $start concatenated with $table_name to create the SQL statement you require
while (($row_1 = $STH_1 -> fetch_assoc()) !== null){
    $table_name = $row_1['table_name']; 
    $stmts_1[] = sprintf("                       
        $start $table_name                         
         WHERE date_time = $today_date ");
    $count = $count + 1;
}

//This statements adds an extra String to the above stmt_1 - the $count variable controls where the extra string is placed, should be at the end of the array. Shouldn't matter though.
//NOTE this is your second query the part 'b' of your above question: variable $table_name_2
    $stmt_1[count] sprintf("                       
            $start2 $table_name_2                         
             WHERE date_time = $today_date ");

//Implode all arrays into one long array adding 'Carriage Return (\n) UNION Carriage Return (\n)' between each statement.

    $stmt_1 = implode("\nUNION\n", $stmts_1);  
    //Add another line at the end for the order by
    $stmt_1 .= "\nORDER BY date_time ASC";    
    $STH_5_2 = $DBH_R->query($stmt_1);  
    while (($row_5_2 = $STH_5_2 -> fetch_assoc()) !== null) {   }  

关于php - mySQL如何合并语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9093980/

相关文章:

java - 如何在Android应用程序中添加表情符号支持以发表评论?

php - Mailgun API 在 Laravel 8.54 中默默失败

PHP/SQL 检查数据库中是否已存在类似条目

php - 在 PHP 中使用 div 标签动态创建网格布局

php - 从多个用户提供的 MySQL 搜索提供 where 子句 - 修复和更好的算法

mysql - 如何为 JBoss 7 数据源配置 MySQL ReplicationDriver?

php - 向 wordpress submit_button 添加一个类

javascript - api 响应返回 "↵"

mysql - 从 WiX Bootstrapper 以被动模式运行 MSI

html - 我想使用 Express js 将数据插入 mysql 数据库,但我无法插入它以及将数据获取到终端