简体   繁体   中英

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

I have two tables Table1 and Table2 .

Table1 contains records of users and products they have purchased.

Table2 contains a list of the products mentioned in Table1 with their 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:

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

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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