php - 在 Android 中从两个表中检索没有重复的数据?

标签 php mysql

我使用以下 sql 语句从两个不同的表中提取数据:

    $query=mysqli_query($con,"SELECT products.pid,products.product_name,products.product_pic,products.product_thumb,products.product_description,products.product_rating,comments.username, comments.comment FROM products RIGHT JOIN comments on products.pid = comments.pid");

我试过:

LEFT JOIN
INNER JOIN
AND JUST JOIN

不幸的是,在 Android ListView 中,如果产品有多个评论,我会得到重复的结果。像这样:

        {
            "pid": "2",
            "product_name": "Some product one",
            "product_pic": "http://localhost/img/generic.png",
            "product_thumb": "",
            "product_description": "some long description",
            "product_rating": "0",
            "username": "john",
            "comment": "one of my favorites"
        },
        {
            "pid": "2",
            "product_name": "Some product one",
            "product_pic": "http://localhost/img/generic.png",
            "product_thumb": "",
            "product_description": "some long description",
            "product_rating": "0",
            "username": "jane",
            "comment": "this was so cool"
        }

如何让 JSON 结果显示在一行中而不是复制产品?

最佳答案

您对 SQL 请求的确切期望是什么?

你想要这样的东西吗?

{
    "pid": "2",
    "product_name": "Some product one",
    "product_pic": "http://localhost/img/generic.png",
    "product_thumb": "",
    "product_description": "some long description",
    "product_rating": "0",
    "comments" : [
        {
            "username": "john",
            "comment": "one of my favorites"
        },
        {
            "username": "jane",
            "comment": "this was so cool"
        }
    ]
}

这对于 SQL 是不可能的,但是您可以将 SQL 响应更改为这种格式。另一种选择是先请求产品,然后调用第二个评论请求。

关于php - 在 Android 中从两个表中检索没有重复的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35811981/

相关文章:

php - Mysql 查询同时使用 LIKE 和 NOT EQUAL 运算符

php - 创建唯一的随机代码 - PHP/MySQL

mysql - 在查询参数中跟踪关系还是在 Django 模型中使用模型属性查找更快?

mysql - 使用单个 mysql 查询确定排名

php - 默认情况下关闭 WooCommerce 产品选项卡

php - 如何存储图像?

php - 如何使用 SimplePie 图像处理程序

php - Pyrocms,在 where 子句上流

mysql - 获取重复项的最早条目

php - 格式化 DateTime Diff 以防止在 PHP 中显示 0 年 0 个月