我想连接两个表,其架构如下所示...... !!
Table :
team_posts
id
team_id
text
location_name
stars_count
comments_count
created_at
updated_at
team_feed_activites
id
team_id
operation
created_at
updated_at
表值为
team_posts
id | team_id | text | location_name | stars_count | comments_count | created_at | updated_at
1 | 23 | crow | india | 1 | 2 | 2016-03-18 06:30:00 | 2016-03-18 06:30:00
2 | 24 | bird | india | 1 | 2 | 2016-03-18 06:30:00 | 2016-03-19 06:30:00
3 | 25 | span | india | 33 | 2 | 2016-03-18 06:30:00 | 2016-03-18 06:30:00
4 | 25 | bang | india | 3 | 2 | 2016-03-18 06:30:00 | 2016-03-12 06:30:00
5 | 27 | crow | india | 1 | 2 | 2016-03-18 06:30:00 | 2016-03-18 06:30:00
6 | 23 | crow | india | 1 | 2 | 2016-03-18 06:30:00 | 2016-01-18 06:30:00
7 | 23 | crow | india | 7 | 2 | 2016-03-18 06:30:00 | 2016-03-18 06:30:00
8 | 23 | hen | india | 1 | 2 | 2016-03-18 06:30:00 | 2016-03-01 06:30:00
9 | 23 | mani | india | 1 | 2 | 2016-03-18 06:30:00 | 2016-03-18 06:30:00
10 | 23 | dog | india | 1 | 2 | 2016-03-18 06:30:00 | 2016-03-18 06:30:00
11 | 29 | crow | india | 10 | 2 | 2016-03-18 06:30:00 | 2016-02-26 06:30:00
12 | 29 | god | india | 1 | 2 | 2016-03-18 06:30:00 | 2016-03-18 06:30:00
13 | 29 | pen | india | 44 | 2 | 2016-03-18 06:30:00 | 2016-03-25 06:30:00
team_feed_activites
id | team_id | operation | created_at | updated_at
1 | 10 | established | 2016-03-18 06:30:00 | 2016-03-18 06:30:00
2 | 23 | established | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
3 | 23 | modified | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
4 | 24 | captian changed | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
5 | 23 | captian added | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
6 | 27 | won | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
7 | 23 | won | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
8 | 23 | paricipated | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
9 | 23 | lost | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
10 | 23 | changed pic | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
11 | 23 | addded image | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
12 | 30 | established | 2016-03-19 06:30:00 | 2016-03-19 06:30:00
现在我需要的是创建一个视图,根据updated_at日期将该表合并到表中。这样我就可以看到每天的团队进展是什么,这样我就可以将其作为团队提供。我可以帮助我一些人如何做到这一点...... !!!在此先感谢.. !!!
答案 0 :(得分:0)
使用简单的INNER JOIN
组合基于updated_at
字段
SELECT * FROM team_posts tp
INNER JOIN team_feed_activites tfa ON tp.updated_at = tfa.updated_at;
答案 1 :(得分:0)
您可以使用此查询
CREATE OR REPLACE FUNCTION testit() RETURNS NUMERIC AS
$BODY$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT * FROM aropenbal ORDER BY doc_date
LOOP
UPDATE aropenbal SET paid_amount = (
CASE WHEN (doc_type IN ('C', 'R')) THEN
(SELECT COALESCE (SUM (arapply_target_paid)* -1, 0)
FROM arapply
WHERE arapply_source_docnumber = r.doc_number
AND arapply_postdate <= r.doc_date)
ELSE
(SELECT COALESCE(SUM (arapply_target_paid),0)
FROM arapply
WHERE arapply_target_docnumber = r.doc_number
AND arapply_postdate <= r.doc_date)
END) WHERE ar_id = r.ar_id;
UPDATE aropenbal SET open_balance = (base_amount - paid_amount)
WHERE ar_id = r.ar_id;
END LOOP;
RETURN (SELECT SUM(open_balance) FROM aropenbal);
END;
$BODY$
LANGUAGE plpgsql;
但小心使用这会给出重复记录一段时间并为每个字段指定新名称,因为字段中有相同的名称,因此它将替换它
字段名称分配:SELECT tp.field,tfa.field FROM team_posts tp,team_feed_activites tfa
答案 2 :(得分:0)
使用LEFT JOIN
和RIGHT JOIN
进行连接,然后使用UNION ALL
加入他们您可以试试这个。
SELECT *
FROM
(SELECT a.* FROM team_posts a
LEFT JOIN
team_feed_activities b
ON a.updated_at=b.updated_at
UNION ALL
SELECT a.* FROM team_posts a
RIGHT JOIN
team_feed_activities b
ON a.updated_at=b.updated_at) subquery
ORDER BY updated_at DESC
注意:两个(子查询)SELECT
列必须具有相同的列数。例如:
SELECT a.*, b.operation
第二,它必须是相同的
SELECT a.*, b.operation
或SELECT a.*, b.team_id
只要两个表都显示相同的列数。
答案 3 :(得分:-1)
我尝试了下面的代码,我接受了输出。
create view team_feed AS
select tp.id as post_id,
tp.team_id as post_team_id,
tp.text,
tp.location_name,
tp.stars_count,
tp.comments_count,
tp.created_at as post_created_at,
tp.updated_at as post_updated_at,
'' as team_feed_activites_id,
'' as team_feed_activites_team_id,
'' as team_feed_activites_operation,
'' as team_feed_activites_created_at,
'' as team_feed_activites_updated_at,
tp.updated_at as event_updated_at
from team_posts as tp
UNION
select '' as post_id,
'' as post_team_id,
'' as text,
'' as location_name,
'' as stars_count,
'' as comments_count,
'' as post_created_at,
'' as post_updated_at,
tfa.id as team_feed_activites_id,
tfa.team_id as team_feed_activites_team_id,
tfa.operation as team_feed_activites_operation,
tfa.created_at as team_feed_activites_created_at,
tfa.updated_at as team_feed_activites_updated_at,
tfa.updated_at as event_updated_at
from team_feed_activites as tfa
在上面的查询中,我基于表选择将唯一列作为event_updated_at,并将其作为视图。并从以下查询中获取数据。
select post_id,
post_team_id,
text,
location_name,
stars_count,
comments_count,
team_feed_activites_id,
team_feed_activites_team_id,
team_feed_activites_operation
from team_feeds
order by event_updated_at ASC