简体   繁体   中英

pull data from two tables with one query/ select statement

I am pulling one column from the USERS table but now I wanna pull columns from the MONEY table. How do I accomplish this?

sample database

USERS TABLE
userID = 33 nestEgg = 600000

MONEY TABLE userID = 33 monthlyContributions = 500, 250, 300 totalContributions =

     <?php
     include 'inc/connect.php';
     $query = "SELECT * FROM USERS WHERE userID = '$userID'";
     $result = mysqli_query($link,$query);
     $row = mysqli_fetch_assoc($result);
     ?>

If I'm not mistaken, you're asking to create a column in MONEY which is the sum of nestEgg and monthlyContributions. I'm a total newbie, but I think getting the sum, followed by the insert, should look like:

SELECT (SELECT SUM(nestEgg) FROM USERS) + (SELECT SUM(monthlyContributions) FROM MONEY)
INSERT INTO MONEY(totalContributions)

If you only want the sum from the monthly contributions, then it should just be:

SELECT SUM(monthlyContributions) FROM MONEY
INSERT INTO MONEY(totalContributions)

If I'm not mistaken.

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