dongtang6681 2013-03-29 15:04
浏览 19
已采纳

Mysql从两个具有相同结构的不同表中选择

can you offer to me something, how to push two different tables but with the same structure, i means all info is the same, just table name is different, mailings and mailings_archive. for one table it is ok

$query = sprintf("SELECT * FROM %smailings  WHERE hidden=0 AND draft=1 %sORDER BY category_id, mailing_segment", $this->dbprefix, $language);

$this->mailings[] = array('id' => $row->id,
                                      'mailing_naam' => $row->mailing_naam,
                                      'mailing_subject' => $row->mailing_subject,
                                      'mailing_from_name' => $row->mailing_from_name,
                                      'mailing_bounceaddress' => $row->mailing_bounceaddress,
                                      'mailing_body' => $row->mailing_body,
                                      'mailing_status' => $row->mailing_status,
                                      'mailing_segment' => $row->mailing_segment,
                                      'mailing_total_subscribers' => $row->mailing_total_subscribers,
                                      'mailing_total_sent' => $row->mailing_total_sent,
                                      'category_id' => $row->category_id,
                                      'utm_campaign' => $row->utm_campaign,
                                      'language' => $row->language);
        }                       

Now it looks like this, how to put in this array, another table data ? Dont forget, every field is the same, just table name different.

  • 写回答

2条回答 默认 最新

  • dqm4675 2013-03-29 15:07
    关注
    (SELECT * FROM %smailings  WHERE hidden=0 AND draft=1 %sORDER BY category_id, mailing_segment)
    UNION
    (SELECT * FROM another_table WHERE hidden=0 AND draft=1 %sORDER BY category_id, mailing_segment)
    
    (select * from table1)
    union
    (select * from table2)
    

    But this will result in one unordered result set, even if you put "order by" inside of each request, but this one probably the fastest.

    If you need to have everything ordered between tables (1....8), then this is a syntax:

    select * from table1
    union
    select * from table2 order by id
    

    If you want to have two separate orders (like 4...8 from one table, 1..4 from another):

    select * from 
    (select * from test1 order by id asc) as t1
    union all
    (select * from test2 order by id asc);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥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