简体   繁体   English

如何将两个表结合在一起以创建JSON | 的PHP

[英]How can two tables be joined together to create a JSON | PHP

I have two tables Table1 and Table2 . 我有两个表Table1Table2

Table1 contains records of users and products they have purchased. Table1包含用户和他们购买的产品的记录。

Table2 contains a list of the products mentioned in Table1 with their price Table2中包含的一个列表products中提到的Table1与他们的price

Example of the table explained above: 上面说明的表格示例:

桌子

I would like to join these two tables in a query that takes the sum of all the sum value of all the purchases made by each user and display that than create a JSON that looks like this: 我想将这两个表连接到一个查询中,该查询采用每个用户进行的所有购买的所有总和的总和并显示,而不是创建如下所示的JSON:

[{"user":"Jack","sumValue":"4.50"},
{"user":"Jake","sumValue":" 4.00 "},{"user":"Mary","sumValue":" 8.50 "}]

I understand how to create the JSON array but don't understand how the SQL can be written 我了解如何创建JSON数组,但不了解如何编写SQL

$sql =  "      What I am missing                  ";

if ($result = mysqli_query($con, $sql))
SELECT User, SUM(Price) as sumValue 
FROM Table1
LEFT JOIN Table2 USING(Product)
GROUP BY User

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM