2013-02-12 77 views
3

數據我有以下表需要單個查詢來從表

 //all users deails 
    smsusers(id,fname , lname ,primary key(id)); 

//message details of users 
//one smsusers can have N messages 
user_messages(messageid,message,adddate ,sentby,visibility, 
userid,primary key(messageid),foreign key(userid) references smsusers(id), 
foreign key(sentby) references smsusers(id)); 


//One message(user_message) can have N comments 
comments(comment_id,comment_on ,commented_by,comment_date, 
comment,foreign key(commented_by) references smsusers(id), 
primary key(comment_id)); 

//one message(user_message) can have N post_images 
post_images(image_id,small_pic_path,userid,messageid, 
foreign key(userid) references smsusers(id),primary key(image_id)); 


//one message(user_message) can have N likes 
likes(element_id,element_type ,liked_by, 
foreign key(liked_by) references smsusers(id) ,adddate, 
primary key(element_id)); 


    //one smsusers(user) can have 1 profile_pic 
profile_pic(pic_id varchar(200),small_pic_path ,userid , 
foreign key(userid) references smsusers(id),primary key(pic_id)); 

我想獲取的user_messages的任何郵件ID和用戶ID下面的細節

1)all details from user_message, 
    2)last 05 comments related to messageid in ascending order from comments table 
     (one message can have multiple comments)which includes comment_id ,comment, 
     comment_date,and details of commented_by(fname,lname,small_pic_path). 
    3)all small_pic_path from post_images(one message can have multiple images), 
    4)total likes from like table, 
    5)all details (smsusers.*,profile_pic.*) of sentby(of table user_messages) 

我想獲取所有這些細節。

我應該使用查詢還是函數來獲取所有這些信息?

請建議查詢或函數來獲取所有數據。

我使用MySQL數據庫和struts2的

+0

您可以針對您要查詢的查詢提供詳細的問題陳述嗎?正如在你想要的字段中,當你說'評論表 (一條消息可以有多條評論)'時,最新的10條評論與messageid相關,' – Incognito 2013-02-12 10:06:16

+0

我提到了5分,因爲我想要結果。 – 2013-02-12 10:11:20

+0

因此,通過'最新的10條評論....'你只需要'comment'表中的'comment'列的值? – Incognito 2013-02-12 10:14:17

回答

0

1)all details from user_message

SELECT * FROM user_messages WHERE userid = <userID> AND messageid = <messageID>; 

2)last 10 comments related to messageid in ascending order from comments table (one message can have multiple comments)which includes comment_id ,comment, comment_date,and details of commented_by(fname,lname,small_pic_path).

SELECT a.comment_id, a.comment, a.comment_date, b.fname || b.lname || c.small_pic_path "Commented by" 
FROM comments a, smusers b, profile_pic c, user_messages d 
WHERE d.messageid = <messageID> 
AND d.userid = b.id 
AND b.id = c.userid 
ORDER BY comment_date 
LIMIT 0, 10; 

3)all small_pic_path from post_images(one message can have multiple images),

​​

4)total likes from like table,

SELECT * FROM likes; 

5)all details (smsusers.*,profile_pic.*) of sentby

You have not posted the structure of sentby 
+0

sentby是一個外鍵在user_messages和主鍵smsusers – 2013-02-13 06:39:00

+0

感謝您的回覆,但我希望所有作爲一個查詢或一個功能如何做到這一點 – 2013-02-13 06:40:13

0

4)如表總喜歡

SELECT count(*) AS total_likes FROM likes WHERE element_id = <messageID>; 

5)所有細節(smsusers *,的profile_pic。*) sentby

SELECT smsusers.*,profile_pic.* FROM user_messages 
    JOIN smsusers 
     ON user_messages.sentby = smsusers.id 
    JOIN profile_pic.userid = smsusers.id 
WHERE user_messages = <messageID> 

現在加入他們都在同一個查詢

SELECT (<Query_1>),(<Query_2>),[...],(<Query_N>) 
+0

感謝您的答覆。我得到了點我會嘗試它 – 2013-02-21 11:16:29

+0

MokroB我試過,但它不worikng。我發現類似的東西請參閱[sqlfiddle](http://sqlfiddle.com/#!2/30321/16) – 2013-02-23 14:34:36

-1

你似乎已經得到了答案。 讓我知道那是怎麼回事,如果你需要別的東西。

+0

Subrojit我得到了Idea。我正在嘗試但沒有獲得輸出 – 2013-02-23 12:37:41