简体   繁体   English

复杂的SQL查询,一对多关系

[英]Complex SQL query, one to many relationship

I have a query such that I need to get 我有一个查询,我需要得到

  1. A specific dog 一只特定的狗
  2. All comments relating to that dog 有关该狗的所有评论
  3. The user who posted each comment 发表每个评论的用户
  4. All links to images of the dog 所有链接到狗的图片
  5. the user who posted each link 发布每个链接的用户

I've tried a several things, and can't figure out quite how to work it. 我已经尝试了几件事,但无法弄清楚如何工作。 Here's what I have (condensed so you don't have to wade through it all): 这就是我所拥有的(浓缩,因此您不必费力地浏览所有内容):

 SELECT s.dog_id,
        s.name,
        c.comment,
        c.date_added AS comment_date_added,
        u.username AS comment_username,
        u.user_id AS comment_user_id,
        l.link AS link,
        l.date_added AS link_date_added,
        u2.username AS link_username,
        u2.user_id AS link_user_id
 FROM dogs AS d
 LEFT JOIN comments AS c
 ON c.dog_id = d.dog_id
 LEFT JOIN users AS u
 ON c.user_id = u.user_id
 LEFT JOIN links AS l
 ON l.dog_id = d.dog_id
 LEFT JOIN users AS u2
 ON l.user_id = u2.user_id
 WHERE d.dog_id = '1'

It's sorta close to working, but it'll only return me the first comment, and the first link all as one big array with all the info i requested. 这有点接近工作了,但只会返回我的第一条评论,并且第一条链接都作为一个大数组,包含我请求的所有信息。 The are multiple comments and links per dog, so I need it to give me all the comments and all the links. 每只狗有多个评论和链接,因此我需要它给我所有评论和所有链接。 Ideally it'd return an object with dog_id, name, comments(an array of the comments), links(an array of the links) and then comments would have a bunch of comments, date_added, username, and user_id and links would have a bunch of links with link, date_added, username and user_id. 理想情况下,它将返回一个带有dog_id,名称,注释(注释数组),链接(链接数组)的对象,然后注释将包含一堆注释,date_add,用户名和user_id,链接将包含一个一堆带有链接,日期添加,用户名和用户ID的链接。 It's got to work even if there are no links or comments. 即使没有链接或评论,它也必须工作。

I learned the basics of mySQL somewhat recently, but this is pretty far over my head. 我最近刚学了mySQL的基础知识,但这远远超出了我的理解。 Any help would be wonderful. 任何帮助都会很棒。 Thanks! 谢谢!

you can't query this in 1 sql query without a lot of redundant data being sent back. 如果没有大量冗余数据发送回,则无法在1个sql查询中查询。 because comments and links bear no relation to each other, you'd be best off doing those calls separately. 由于注释和链接之间没有关系,因此最好分开进行这些调用。

so 3 calls in total : 1 to retrieve the dog data, 1 to retrieve comments/user, and 1 to retrieve links/user. 因此,总共进行了3次调用:1检索狗数据,1检索评论/用户,1检索链接/用户。

SELECT d.dog_id, d.name FROM dogs AS d WHERE d.dog_id = '1';

SELECT c.comment, c.date_added AS comment_date_added,
       u.username AS comment_username, u.user_id AS comment_user_id
       FROM comments AS c LEFT JOIN users AS u ON c.user_id = u.user_id
       WHERE c.dog_id = '1';

SELECT l.link AS link, l.date_added AS link_date_added,
    u.username AS link_username, u.user_id AS link_user_id
       FROM links AS l LEFT JOIN users AS u ON l.user_id = u.user_id
       WHERE l.dog_id = '1';

the reason for this is that although you can join the dogs/comments table well enough, when you start trying to join in the links - how do you relate a link to a comment? 这样做的原因是,尽管您可以很好地加入dogs / comments表,但是当您开始尝试加入链接时,如何将链接与评论关联起来? there's no obvious join so your only way to do it is to use an OUTER JOIN. 没有明显的联接,因此您唯一的方法是使用外部联接。 this will lead to a lot of redundant data in your calls. 这会在您的通话中导致大量冗余数据。

also, this kind of thing makes caching a lot easier and more intuitive. 同样,这种事情使缓存变得更加容易和直观。 in my experience it's usually easiest to go with a couple of natural queries than a spaghetti join. 以我的经验,通常最简单的方法是进行几次自然查询,而不是加入意大利面条。 :) :)

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

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