【问题标题】:return data from different table sql to html将数据从不同的表sql返回到html
【发布时间】:2012-02-06 22:19:51
【问题描述】:

我的用户有页面:

  • 用户收藏
  • 用户图片

我尝试做一个页面“一切”,它将从 2 个 sql 表(已经有 sql 代码)获取数据到一个按日期排序的 html 页面。问题是“用户图像”有一个 html 结构,而“用户收藏”有另一个。许多网站都有这样的页面,其中从不同的表中以不同的 html 结构输出不同的数据。今天我第一次尝试这样做,不知道正确的方法。

我的桌子:

用户

  • 身份证
  • 用户名

图片

  • 身份证
  • user_id
  • 图片
  • 说明
  • 日期

user_favorites

  • 身份证
  • user_id
  • image_id
  • 日期

我用这个 sql 获取用户图像

function users_pictures($user_id)
{
    $sql = "SELECT username as user, p.image as user_image, i.image, i.id as image_id, i.description as text, UNIX_TIMESTAMP(i.date) as image_date, COALESCE ( imgcount.cnt, 0 ) as comments
            FROM users u
            LEFT JOIN images i ON i.user_id = u.id
            LEFT JOIN images p ON p.id = (SELECT b.id FROM images AS b where u.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 i.id = imgcount.image_id
            WHERE i.user_id = ?
            ORDER BY i.date DESC";

    $query = $this->db->query($sql, $user_id);

    return $query->result_array();
}

我得到所有用户图片和用户当前图片-头像(最后上传的图片是用户头像)

返回示例:

[images_list] => Array
    (
        [0] => Array
            (
                [user] => 8888
                [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [image] => http://127.0.0.1/auth_system_1/upload_images/224/224_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [image_id] => 4
                [text] => 
                [image_date] => 50 minutes
                [comments] => 0
                [user_first_image] => 1
            )

    )

用户最喜欢的表是:

function users_favorites_list($user_id)
{
    $sql = "SELECT
                    u.username as user, 
                    p.image as user_image,
                    fav.id as favorite_id, 
                    UNIX_TIMESTAMP(fav.date) as favorite_date, 
                    i.id as images_id,
                    i.image,
                    i.description as text,
                    u2.username as favorite_user,
                    t.image as favorite_user_image

            FROM users u
            LEFT JOIN user_favorites fav ON fav.user_id = u.id
            LEFT JOIN user_follow f ON f.follow_id = fav.user_id
            LEFT JOIN images i ON i.id = fav.image_id
            LEFT JOIN users u2 ON u2.id = i.user_id
            LEFT JOIN images p ON p.id = (SELECT b.id FROM images AS b where fav.user_id = b.user_id ORDER BY b.id DESC LIMIT 1)
            LEFT JOIN images t ON t.id = (SELECT b.id FROM images AS b where u2.id = b.user_id ORDER BY b.id DESC LIMIT 1)
            WHERE fav.user_id = ?
            GROUP BY fav.id
            ORDER BY fav.date DESC";

    $query = $this->db->query($sql, array($user_id, $user_id));

    return $query->result_array();        
}

返回示例:

Array
(
    [0] => Array
        (
            [user] => 8888
            [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
            [favorite_id] => 5
            [favorite_date] => 18 minutes ago
            [images_id] => 2
            [image] => http://127.0.0.1/auth_system_1/upload_images/100/100_flw3utn9igiqh7dtt2o61ydf8_174.jpeg
            [text] => 3
            [favorite_user] => 6666
            [favorite_user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_flw3utn9igiqh7dtt2o61ydf8_174.jpeg
        )

    [1] => Array
        (
            [user] => 8888
            [user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
            [favorite_id] => 2
            [favorite_date] => 1 week ago
            [images_id] => 4
            [image] => http://127.0.0.1/auth_system_1/upload_images/100/100_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
            [text] => 
            [favorite_user] => 8888
            [favorite_user_image] => http://127.0.0.1/auth_system_1/upload_images/24/24_0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
        )

)

![我尝试从 2 sql 返回数据的 html 结构][1]

我需要从具有不同 html 结构的一页中的 2 个表中选择用户活动(示例参见图片)。我想很多人都会这样做。请告诉我该怎么做?

[everything_list] => Array
    (
        [0] => Array
            (
                [user] => 8888
                [user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [favorite_id] => 5
                [favorite_date] => 1328565406
                [images_id] => 2
                [image] => flw3utn9igiqh7dtt2o61ydf8_174.jpeg
                [text] => 3
                [favorite_user] => 6666
                [favorite_user_image] => flw3utn9igiqh7dtt2o61ydf8_174.jpeg
            )

        [1] => Array
            (
                [user] => 8888
                [user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [favorite_id] => 2
                [favorite_date] => 1327856547
                [images_id] => 4
                [image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
                [text] => 
                [favorite_user] => 8888
                [favorite_user_image] => 0j1kjjzdv3ez07a0ee4lnmjb7_163.jpeg
            )

    )

【问题讨论】:

    标签: php mysql sql


    【解决方案1】:

    下面的查询返回你需要什么?

    SELECT 
        username as user, 
        p.image as user_image, 
        i.image, 
        i.id as image_id, 
        i.description as text, 
        UNIX_TIMESTAMP(i.date) as image_date, 
        COALESCE ( imgcount.cnt, 0 ) as comments,
        fav.id as favorite_id,
        UNIX_TIMESTAMP(fav.date) as favorite_date,
        u2.username as favorite_user,
        t.image as favorite_user_image
    FROM users u
    LEFT JOIN user_favorites fav ON fav.user_id = u.id
    LEFT JOIN user_follow f ON f.follow_id = fav.user_id
    LEFT JOIN images i ON i.user_id = u.id
    LEFT JOIN users u2 ON u2.id = i.user_id
    LEFT JOIN images p ON p.id = (SELECT b.id FROM images AS b where u.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 i.id = imgcount.image_id
    LEFT JOIN images t ON t.id = (SELECT b.id FROM images AS b where u2.id = b.user_id     ORDER BY b.id DESC LIMIT 1)
    WHERE u.user_id = ?
    GROUP BY fav.id
    ORDER BY i.date DESC
    

    【讨论】:

    • 为什么需要限制?我有 3 页... 1) 用户 iamge 页面 2) 使用收藏夹页面 3) 一切。这里介绍的 SQL 来自 1 和 2 ...我不知道如何从表和输出中获取数据到具有不同 html 结构的页面“所有内容”
    • 您说“我需要选择 3 个项目(1 个来自“images”表,2 个来自“user_favorites”)”。因此,一张表中的 1 条记录和另一张表中的 2 条记录。这就是您可以使用 LIMIT 的原因,否则您将在不需要的情况下查询表中的所有数据。
    • ma表有1条和2条记录
    • 好的,但实际上你需要加入 user_favorites 的图像的所有数据,是吗?
    • 是的。制作一个大的 sql 是个问题,但没有 foreach 所有活动图像、带有 html 的收藏图像那么大
    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多