1

I have 2 tables, I need to sum up the total value from each tables price column. I did some research on Stack but the solutions I saw were too long and did not seems efficient enough code. With MySQL I am trying to get the total value from StoreCoins plus the total value from StoreMemorabilia and the total value from supplies then I want the sum of the total of both table's column. This first code is currently not working...

if($results = $this->dbConn->query("SELECT SUM(column) AS InventoryValue FROM (
                                        SELECT SUM(column) AS value FROM StoreCoins
                                        UNION ALL
                                        SELECT SUM(column) AS value FROM StoreMemorabilia
                                        ) allposts")){
while($data = $results->fetch_assoc()){
    $totalVal = $data['InventoryValue'];
    $totalVal .= ".00";

I need to know what I am doing wrong in the code above.

On another note (this is php related): I have the following script that once the correct value is returned it decides where to put the commas for dollar values. I would like a recommendation to a much more efficient way to create this function and its results. The switch statement works just fine and does what I need it to do, I just want a more eloquent way of writing this.

switch(strlen($Val)){
    case 7:
        $rem = substr($Val, 1, 6);
        $Val = substr_replace($Val, ",", 1);
        $Val = $Val.$rem;
        break;
    case 8:
        $rem = substr($Val, 2, 6);
        $Val = substr_replace($Val, ",", 2);
        $Val = $Val.$rem;
        break;
    case 9:
        $rem = substr($Val, 3, 6);
        $Val = substr_replace($Val, ",", 3);
        $Val = $totalVal.$rem;
        break;
    case 10:
        $rem = substr($Val, 1, 10);
        $Val = substr_replace($Val, ",", 1);
        $Val = $Val.$rem;
        $rema = substr($Val, 5, 6);
        $Val = substr_replace($Val, ",", 5);
        $Val = $Val.$rema;
}

2 Answers 2

2

I hope the following is what you are after:

select (
    ( select sum(`price`) from `StoreCoins` ) 
        + 
    ( select sum(`price`) from `StoreMemorabilia` )
) as 'total';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I tested this as a second option and it works. 2 good ways to do this.
1

Change the query like this

SELECT SUM(column) FROM ( 
SELECT SUM(column) AS value FROM StoreCoins

UNION 

SELECT SUM(column) AS value FROM StoreMemorabilia
) InventoryValue

6 Comments

I tried it but no luck. It does not display. Just disappears.
@RookieRecruits Can you post screen shot of your tabels
I cannot, doesn't seem to let me.
@Rookie Recruits you need to replace column in the subqueries with the names of your columns in your tables you want to count, then you want to sum them in your main query by referring to whatever you named the columns in your subquery.
@Rookie Recruits Then change column to price in the subqueries, and column to value in your top query to sum your calculated columns.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.