简体   繁体   English

MySQL返回相同数据的不同行

[英]MySQL returning same data different rows

I am trying to get data from reviews table then, join the same table again but find different data (likes) based on the id of the first table (reviews) and find the corresponding description from yet another joined table (descriptions). 我正在尝试从评论表中获取数据,然后再次加入同一张表,但根据第一个表(评论)的ID查找不同的数据(喜欢),并从另一个联接表(描述)中找到相应的描述。

I know this might be hard to visualize but maybe someone will know why the likes from the second reviews table return the same data for different rows: 我知道这可能很难想象,但也许有人会知道为什么第二条评论表中的点赞会针对不同的行返回相同的数据:

SELECT r.title, co.likes, d.description
FROM reviews r
INNER JOIN reviews co
INNER JOIN reviews_descriptions d
ON co.id = d.review_id
WHERE co.parent = 52
AND r.id = 52;

The result is two rows in which title and likes have the same data while the description field grabs different data (the correct way). 结果是两行,其中标题和喜欢的数据相同,而描述字段获取的数据不同(正确的方式)。 Likes should have different data for each row. 点赞的每一行应具有不同的数据。

Help please. 请帮助。

An alternative for writing this and I believe this is what you are looking for: 一种替代方法,我相信这是您正在寻找的东西:

SELECT r.title, co.likes, d.description
FROM reviews r, reviews co, review_descriptions d
WHERE co.parent = r.id AND d.review_id = co.id AND r.id = 52;

Since you are matching co.parent to 52 and r.id to 52, then you should have a join on co.parent = r.id, however, you should rarely have to do a join on the same table. 由于将co.parent匹配为52,将r.id匹配为52,因此您应该在co.parent = r.id上具有联接,但是,您几乎不必在同一表上进行联接。 I think your tables are poorly formatted or you don't need to do the join in the first place and should be able to use r.likes instead of co.likes. 我认为您的表格格式不正确,或者您不需要首先进行连接,并且应该能够使用r.likes代替co.likes。

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

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