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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器