简体   繁体   English

多个表结合在mysql中

[英]multiple tables combine in mysql

I have many tables in my server and they are name with the date. 我的服务器中有很多表,它们是带有日期的名称。 Example e_20150916 , e_20150917 , e_20150918 , e_20150919 .....etc 例子e_20150916e_20150917e_20150918e_20150919 ..... etc

I want to do this table when user add date in my input type..... example when he wants the total summary between 2015/08/1 to 2015/08/15. 当用户在我的输入类型中添加日期时,我想创建此表.....例如,当他希望在2015/08/1至2015/08/15之间进行汇总时。 Help me with these query how to do this. 通过这些查询帮助我,该怎么做。

I am doing this with 2 table combine. 我正在用2表结合。

Here my query for 2 tables 在这里我查询2表

SELECT  e.cus,e.cusname,
        e.gid,
        SUM(total)
FROM ( 
    SELECT
        e.cus,
        e.cusname,
        e.gid,
        ROUND(SUM(TIMEDIFF(FROM_UNIXTIME(stoptime / 1000),
                        FROM_UNIXTIME(starttime / 1000))) / 60, 4) as total
    FROM
        e_cdr_$table
    WHERE e.cusname LIKE '%$customername%'
        UNION ALL
        SELECT
        e.cus,
        e.cusname,
        e.gid,
        ROUND(SUM(TIMEDIFF(FROM_UNIXTIME(stoptime / 1000),
                        FROM_UNIXTIME(starttime / 1000))) / 60, 4) as total
    FROM                                
        e_cdr_$table1
    WHERE e.cusname LIKE '%$customername%'
) s

I need to do this query for 7 tables. 我需要对7个表进行此查询。 How can I do it? 我该怎么做?

As pointed out in the comments before, it is not the most clever approach to have a separate table for each date, but if that is the situation you need to live with you could at least do all the UNION ALL action in a sub query and then do the actual selection and SUM -magic with a single WHERE clause like: 正如之前的评论中所指出的那样,对于每个日期都有一个单独的表并不是最聪明的方法,但是如果是这种情况,那么您需要与之共处,至少可以在子查询中执行所有UNION ALL操作,然后使用单个WHERE子句进行实际选择和SUM -magic:

SELECT  cus,cusname,gid,
        ROUND(SUM(TIMEDIFF(FROM_UNIXTIME(stoptime / 1000),
                    FROM_UNIXTIME(starttime / 1000))) / 60, 4) as total
FROM ( 
  SELECT * FROM e_20150916 UNION ALL
  SELECT * FROM e_20150917 UNION ALL
  SELECT * FROM e_20150918 UNION ALL
  SELECT * FROM e_20150919 UNION ALL
  SELECT * FROM e_20150920 UNION ALL
  SELECT * FROM e_20150921 UNION ALL
  SELECT * FROM e_20150922 ) tbls
WHERE cusname LIKE '%$customername%'
GROUP BY cus,cusname,gid

I am not sure about performance issues. 我不确定性能问题。 If it should turn out that your tables are prohibitively large then you can of course limit their scope individually by doing: 如果应该证明您的表太大,那么您当然可以通过以下方式分别限制其范围:

SELECT  cus,cusname,gid,
        ROUND(SUM(TIMEDIFF(FROM_UNIXTIME(stoptime / 1000),
                    FROM_UNIXTIME(starttime / 1000))) / 60, 4) as total
FROM ( 
  SELECT * FROM e_20150916 WHERE cusname LIKE '%$customername%' UNION ALL
  SELECT * FROM e_20150917 WHERE cusname LIKE '%$customername%' UNION ALL
  SELECT * FROM e_20150918 WHERE cusname LIKE '%$customername%' UNION ALL
  SELECT * FROM e_20150919 WHERE cusname LIKE '%$customername%' UNION ALL
  SELECT * FROM e_20150920 WHERE cusname LIKE '%$customername%' UNION ALL
  SELECT * FROM e_20150921 WHERE cusname LIKE '%$customername%' UNION ALL
  SELECT * FROM e_20150922 WHERE cusname LIKE '%$customername%') tbls
GROUP BY cus,cusname,gid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM