简体   繁体   English

再添加一个表来查询和计算数据SQL

[英]Add one more table to query and count the data SQL

I try to select data from 4 table (last table need to count data) 我尝试从4个表中选择数据(最后一个表需要计算数据)

My MySQL tables structure 我的MySQL表结构

users 用户

id
username

images 图片

id
user_id
image

user_follow user_follow

id
user_id
follow_id

commentaries 评论

id
user_id
image_id
text

I have got this SQL query: 我有这个SQL查询:

    $sql = "SELECT u.username as user, i.image as user_image, p.image, p.date
            FROM users u              
            LEFT JOIN user_follow f ON u.id = f.follow_id
            LEFT JOIN images p ON p.user_id = u.id
            LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
            WHERE f.user_id = 3 OR p.user_id = 3       
            ORDER BY p.date DESC";

This line return user current image (last image) 此行返回用户当前图像(最后一张图像)

 LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)

it returns all images from me and my friends 它返回我和我的朋友的所有图像

[0] => Array
    (
        [user] => 8888
        [user_image] => second.jpg
        [image] => second.jpg
        [date] => 2012-01-24 14:42:27
    )

[1] => Array
    (
        [user] => 8888
        [user_image] => second.jpg
        [image] => first.jpg
        [date] => 2012-01-24 14:42:27
    )

[2] => Array
    (
        [user] => 3333
        [user_image] => ax46l7v7vugnesk10whk_339.jpg
        [image] => ax46l7v7vugnesk10whk_339.jpg
        [date] => 2012-01-24 01:54:19
    )

[3] => Array
    (
        [user] => 3333
        [user_image] => ax46l7v7vugnesk10whk_339.jpg
        [image] => aaaaaaaa.jpg
        [date] => 2012-01-24 01:49:57
    )

I tried to add 我试着补充一下

 left join commentaries c ON c.user_id = u.id

and result was 结果是

[2] => Array
    (
        [user] => 3333
        [user_image] => ax46l7v7vugnesk10whk_339.jpg
        [image] => ax46l7v7vugnesk10whk_339.jpg
        [date] => 2012-01-24 01:54:19
        [id] => 1
    )

[3] => Array
    (
        [user] => 3333
        [user_image] => ax46l7v7vugnesk10whk_339.jpg
        [image] => ax46l7v7vugnesk10whk_339.jpg
        [date] => 2012-01-24 01:54:19
        [id] => 2
    )

[4] => Array
    (
        [user] => 3333
        [user_image] => ax46l7v7vugnesk10whk_339.jpg
        [image] => aaaaaaaa.jpg
        [date] => 2012-01-24 01:49:57
        [id] => 1
    )

[5] => Array
    (
        [user] => 3333
        [user_image] => ax46l7v7vugnesk10whk_339.jpg
        [image] => aaaaaaaa.jpg
        [date] => 2012-01-24 01:49:57
        [id] => 2
    )

Duplicate user if it have commentaries (By the way [user] => 3333 have 2 comments in example) 如果有注释,则重复用户(顺便提一下[user] => 3333在示例中有2条评论)

I am trying to add one more table "commentaries" and count how many commentaries have every picture (from me and my friends) if no commentaries with such $user_id then return 0 我正在尝试添加一个表“评论”并计算每个图片(来自我和我的朋友)有多少评论如果没有这样的$ user_id的评论然后返回0

You really know how to make a question confusing. 你真的知道如何让一个混乱的问题。 The issue here is that you're not understanding the consequences of your joins. 这里的问题是你不了解你的联接的后果。

Using your example. 用你的例子。 You have a table (users in this case), that has one to many relationships to two other tables (images and commentaries). 你有一个表(在这种情况下是用户),与其他两个表(图像和注释)有一对多的关系。

Such as this: 比如这样:

         users
        /     \
  images       commentaries

When you try to join both of these related tables to your base table simultaneously, the effect is to produce the equivalent of a full outer join between the two child tables. 当您尝试同时将这两个相关表连接到基表时,效果是产生两个子表之间的完全外连接的等效项。

This: 这个:

SELECT *
FROM users u
LEFT JOIN images p ON p.user_id = u.id
LEFT JOIN commentaries c ON c.user_id = u.id

is precisely the same as this: 与此完全相同:

SELECT *
FROM images p
LEFT JOIN commentaries c ON c.user_id = p.user_id

(in terms of the number of records produced) (就产生的记录数量而言)

It would be fine if one of the child tables had a 1 to 1 relationship with the parent table, but they don't. 如果其中一个子表与父表具有1对1的关系,那就没关系,但它们没有。 Since they both have multiple records in them, the effect is a FULL OUTER JOIN and the result is a multiplication of the number of records produced in the output. 由于它们都有多个记录,因此效果是FULL OUTER JOIN,结果是输出中产生的记录数的乘积。 The output will contain a number or records equal to the number of matching records in the one table multiplied by the number of matching records in the other table. 输出将包含一个或多个记录,这些记录等于一个表中匹配记录的数量乘以另一个表中匹配记录的数量。 Thus, since you have two records in each table matching that user_id, the result set contains four records. 因此,由于每个表中有两个匹配该user_id的记录,因此结果集包含四个记录。

It's difficult to understand the last part of your question, so some clarification would be nice. 很难理解你问题的最后部分,所以一些澄清会很好。 It seems as though you're trying to only count the records from one table or the other, although frankly I'm uncertain which. 看起来好像你只想从一张桌子或另一张桌子上算出记录,虽然坦率地说我不确定哪一张。

The following line is extremely confusing. 以下行非常混乱。

count how many commentaries have every picture 计算每张照片有多少评论

If you do, in fact, only wish to count the records from one table or the other, then grouping will solve your multiplication issue. 事实上,如果您只想从一个表或另一个表中计算记录,那么分组将解决您的乘法问题。

$sql = "SELECT u.username as user, i.image as user_image, p.image, p.date, c.commentcount
        FROM users u              
        LEFT JOIN user_follow f ON u.id = f.follow_id
        LEFT JOIN images p ON p.user_id = u.id
        LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
        LEFT JOIN (SELECT x.user_id, COUNT(*) AS commentcount FROM commentaries x GROUP BY x.user_id) c ON c.user_id = u.id
        WHERE f.user_id = 3 OR p.user_id = 3       
        ORDER BY p.date DESC";

The trouble with this, as was commented earlier, is that it has essentially nothing to do with the images. 正如之前评论的那样,问题在于它与图像基本上没有任何关系。 There is no discernible direct link between images and commentaries. 图像和评论之间没有明显的直接联系。 Only a many to many relationship exists between them via the users table. 通过users表,它们之间只存在多对多的关系。 So, it's very difficult for me to be certain this helps you at all. 所以,我很难确定这对你有帮助。

Essentially, this only gives you the number of comments a user has. 从本质上讲,这只会为您提供用户拥有的评论数量。 It has nothing to do with images. 它与图像无关。 You could use very similar code to tell you how many images a user has, but that has nothing to do with the comments. 您可以使用非常相似的代码来告诉您用户拥有多少图像,但这与注释无关。

If what you wish to do, is determine how many of one the user has, if the user has the other at all, then this will answer that question. 如果你想做什么,确定一个用户有多少,如果用户有另一个,那么这将回答这个问题。 You would simply add a test in the WHERE clause to determine if the user has the required related record or not. 您只需在WHERE子句中添加一个测试,以确定用户是否具有所需的相关记录。

If you can clarify your intent, I may be able to help more. 如果你能澄清你的意图,我可以提供更多帮助。 Otherwise, I hope this helps. 否则,我希望这会有所帮助。

You need to use GROUP BY to count rows in groups (in your case comments for every image). 您需要使用GROUP BY来计算组中的行数(在您的情况下,每个图像的注释)。 This query should do the trick: 这个查询应该做的伎俩:

SELECT u.username as user, i.image as user_image, p.image, p.date,
            COALESCE ( imgcount.cnt, 0 ) as comments
            FROM users u              
            LEFT JOIN user_follow f ON u.id = f.follow_id
            LEFT JOIN images p ON p.user_id = u.id
            LEFT JOIN images i ON i.id = (SELECT b.id FROM images AS b where p.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
            LEFT JOIN 
            ( SELECT image_id, COUNT(*) as cnt FROM
                 commentaries 
              GROUP BY image_id  ) imgcount
            ON p.id = imgcount.image_id
            WHERE f.user_id = 3 OR p.user_id = 3       
            ORDER BY p.date DESC

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

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