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 DB と struts2 を使用しています

4

3 に答える 3

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),

SELECT small_pic_path
FROM post_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
于 2013-02-12T12:07:30.833 に答える
0

4)同じようなテーブルからの合計いいね、

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

5) sentbyのすべての詳細(smsusers。*、profile_pic。*)

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>

今、それらすべてを1つのクエリに参加させます

SELECT (<Query_1>),(<Query_2>),[...],(<Query_N>)
于 2013-02-21T11:11:40.927 に答える
-1

あなたは答えを得たようです。それがどうだったのか、他に何か必要なことがあれば教えてください。

于 2013-02-23T08:36:13.643 に答える