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

时间:2016-03-05 08:39:14

标签: php mysql

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

public class Test {

public static void main(String args[]){
long l1 =6788819226L;
float f1= l1;
long l2= (long)f1;
System.out.println("Input long::"+l1);
System.out.println("Float value::"+f1);
System.out.println("Typecasted value::"+l2);
}
}
      Output of for second class:
      Input long::6788819226
      Float value::6.7888195E9
      Typecasted value::6788819456

我试过了:

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

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

LEFT JOIN
INNER JOIN
AND JUST JOIN

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

1 个答案:

答案 0 :(得分:2)

您对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响应更改为此格式。另一种选择是先请求产品,然后再发出第二次评论请求。