doujie9252 2014-12-11 17:16
浏览 71
已采纳

MySQL从多个相同的表中选择查询并确定结果中的表

I have multiple tables with identical columns and different data in them and i'm trying to make a single query to pull the data from all those tables and treat them as a single query. I also need to know from which table is the specific result. I found this to help me get a query from multiple tables, but when i try

$tables=array("loads", "loads_completed");
foreach ($tables as $table) {
  $table = '`'.mb_ereg_replace('`','``',$table).'`';
  $sql[] = "(
    SELECT   *
    FROM  $table
    ORDER BY status
    LIMIT    50
  )";
}
$sql = implode(' UNION ALL ', $sql);

i get Query was empty even though there is content in both tables.

This is what i found for displaying the result table, but i'm not sure how to form the query with array of tables.

  • 写回答

1条回答 默认 最新

  • duanjuebin2519 2014-12-11 18:40
    关注

    You can do by this way

    foreach ($tables as $table) {
      $table = '`'.mb_ereg_replace('`','``',$table).'`';
      $sql[] = "(
        SELECT   *,'".$table."' AS tableName
        FROM  $table
        ORDER BY status
        LIMIT    50
      )";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog