dongshuo1856 2016-03-05 08:39
浏览 36
已采纳

在Android中从两个表中检索数据而不重复?

I am pulling data from two different tables using the following sql statement:

    $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");

I've tried:

LEFT JOIN
INNER JOIN
AND JUST JOIN

Unfortunately, in the Android listview, I get duplicate results of a product if it has more than one comment. Like so:

        {
            "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"
        }

How do I get the JSON result to display in one row instead of duplicating the product?

  • 写回答

1条回答 默认 最新

  • dqol6556 2016-03-07 00:29
    关注

    What exactly do you expect from the SQL-request?

    Do you want something like this?

    {
        "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"
            }
        ]
    }
    

    This is not possible with SQL, but you can change the SQL-response to this format. An other option is to request, the products first, and call a second request for comments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式