gpt4 book ai didi

mysql - 从表中获取数据所需的单个查询

转载 作者:行者123 更新时间:2023-11-29 01:05:33 25 4
gpt4 key购买 nike

我有以下表格

     //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 的任何 messageid 和 userid 的以下详细信息

    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

最佳答案

1) 来自 user_message 的所有详细信息

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

2)评论表中与 messageid 相关的最后 10 条评论按升序排列
(一条消息可以有多个评论)其中包括 comment_id ,comment,
comment_date,以及 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(一条消息可以有多个图片),

SELECT small_pic_path
FROM post_images;

4)点赞表中的点赞总数

SELECT * FROM likes;

5)sentby 的所有详细信息(smsusers.*,profile_pic.*)

You have not posted the structure of sentby

关于mysql - 从表中获取数据所需的单个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com