我使用以下 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/