我有5个包含以下属性的表,
+--------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+-------+
| actor_id | int(11) | YES | MUL | NULL | |
| activity_object_id | int(11) | YES | MUL | NULL | |
| interest_level | tinyint(4) | YES | | 10 | |
| feed_view | smallint(6) | YES | | 0 | |
| quick_view | smallint(6) | YES | | 0 | |
| page_view | smallint(6) | YES | | 0 | |
| fullscreen_view | smallint(6) | YES | | 0 | |
| reserved1 | int(11) | YES | | NULL | |
| reserved2 | int(11) | YES | | NULL | |
| reserved3 | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+--------------------+-------------+------+-----+---------+-------+
我们如何创建新的临时表,它是所有5个表值的总和。 activity_object_id 是唯一的,一个表可能包含activity_object_id,而另一个表可能不包含。
table1有一个active_object_id说'gowthamkey',table2有相同的键'gowthamkey',table3可能没有'gowthamkey'。所以我想将所有表值总结到新表中,以便它有一个键'gowthamkey',其中值是以下的总和:
feed_view,quick_view,page_view,fullscreen_view,reserved1,reserved2,reserved3 ,除了actor_id,interest_level,created_at,updated_at。
根据@bummer回答,这是我的查询:
CREATE TABLE actor_activity_object_stats_temp_7_days_12 AS
select actor_id, activity_object_id, interest_level, SUM(feed_view) AS feed_view, SUM(quick_view) AS quick_view, SUM(fullscreen_view) as fullscreen_view
from (
select * from actor_activity_object_stats_temp_2016_04_29
union all,
select * from actor_activity_object_stats_temp_2016_04_30
union all,
select * from actor_activity_object_stats_temp_2016_05_01
union all,
select * from actor_activity_object_stats_temp_2016_05_02
union all,
select * from actor_activity_object_stats_temp_2016_05_03
union all,
select * from actor_activity_object_stats_temp_2016_05_04
union all,
select * from actor_activity_object_stats_temp_2016_05_05
union all,
select * from actor_activity_object_stats_temp_2016_05_06
union all,
select * from actor_activity_object_stats_temp_2016_05_07
union all,
select * from actor_activity_object_stats_temp_2016_05_08 ) AS X
group by activity_object_id
答案 0 :(得分:1)
从$this->db->select('*');
$this->db->from('yourtablename');
$this->db->order_by("column_name", "ASC"); // or lower case asc
// $this->db->limit(1); limits the number of results.
$query = $this->db->get();
return $query->result();
开始,然后使用UNION
添加同一SUM()
所有表格中的所有值。
activity_object_id
答案 1 :(得分:0)
我相信这可以解决你的问题。
create table new_table
as
select * from table1
union
select * from table2
union
select * from table3
union
select * from table4
union
select * from table5)
这里有sql fiddle来说明。