For a piece of coursework I have to create a facebook-like application.
Currently I am working on the 'timeline' page and struggling with the complexity of the SQL query that is required.
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. 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.
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.