简体   繁体   English

Oracle SQL联接/合并2个表并按ID分组/排序以创建类似Facebook的时间轴

[英]Oracle SQL to join / union 2 tables and group / sort by ID to create facebook-like timeline

For a piece of coursework I have to create a facebook-like application. 对于一部分课程,我必须创建一个类似Facebook的应用程序。

Currently I am working on the 'timeline' page and struggling with the complexity of the SQL query that is required. 目前,我正在“时间轴”页面上工作,并努力应对所需的SQL查询的复杂性。

My desired outcome is to display posts and responses in a single table and group or sort so that the responses are below the corresponding post 我期望的结果是在单个表格和组或排序中显示帖子和回复,以便回复位于相应帖子的下方

eg post_id        text
     1          First post
     1          Response 1 to post 1
     1          Response 2 to post 1
     2          Second post
     2          Response 1 to post 2
     2          Response 2 to post 2

Database schema is as follows: 数据库架构如下:

create or replace type post_obj as object (
   post_id number,
   post_text varchar2(500),
   user_posted_by ref user_obj,
   post_date timestamp,
   map member function get_postid return number) not final;

create table post_obj_tbl of post_obj;


create or replace type response_obj as object (
   response_id number,
   response_text varchar2(500),
   post_details ref post_obj,
   user_posted_by ref user_obj,
   response_date timestamp,
   map member function get_responseid return number) not final;

create table response_obj_tbl of response_obj;

So far I have managed to get the two tables to join so that I get a single table displaying posts and responses but it is automatically grouping by the user id. 到目前为止,我设法将两个表连接在一起,以便获得一个显示帖子和回复的表,但该表会自动按用户ID进行分组。 Here is the query: 这是查询:

SELECT p_tbl.user_posted_by.username, p_tbl.post_text, p_tbl.post_date
FROM post_obj_tbl p_tbl

WHERE 

p_tbl.user_posted_by.user_id = (
   SELECT u_tbl.user_id
   FROM user_obj_tbl u_tbl 
   WHERE u_tbl.username = (select V('APP_USER') from DUAL) )

OR

p_tbl.user_posted_by.user_id in (
   SELECT f_tbl.friend_id 
   FROM friends_obj_tbl f_tbl
   WHERE f_tbl.user_id = (
      SELECT u_tbl.user_id
      FROM user_obj_tbl u_tbl 
      WHERE u_tbl.username = (select V('APP_USER') from DUAL) )
   )

UNION
SELECT r_tbl.user_posted_by.username, r_tbl.response_text, r_tbl.response_date
FROM response_obj_tbl r_tbl, post_obj_tbl p_tbl
WHERE r_tbl.post_details.post_id = p_tbl.post_id;

I really hope this makes sense...it's messing with my head! 我真的希望这是有道理的...我的头很乱! Any help much appreciated :) 任何帮助表示赞赏:)

First of all, why do you create tables of objects? 首先,为什么要创建对象表? Usual way is to create tables like this: 通常的方法是创建这样的表:

create table post_obj  (
   post_id number,
   post_text varchar2(500),
   user_posted_by number,
   post_date timestamp);

It is much easier to work with such table, especially in APEX. 使用此类表要容易得多,尤其是在APEX中。
About query. 关于查询。 I'll take one part: 我将参与其中:

SELECT p_tbl.user_posted_by.username, p_tbl.post_text, p_tbl.post_date
FROM post_obj_tbl p_tbl
WHERE 
p_tbl.user_posted_by.user_id = (
   SELECT u_tbl.user_id
   FROM user_obj_tbl u_tbl 
   WHERE u_tbl.username = (select V('APP_USER') from DUAL) )

It can be transformed to: 可以将其转换为:

SELECT p_tbl.user_posted_by.username, p_tbl.post_text, p_tbl.post_date
FROM   post_obj_tbl p_tbl,
       user_obj_tbl u_tbl
WHERE  p_tbl.user_posted_by = u_tbl.user_id
   AND u_tbl.username = V('APP_USER')

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

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