Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql count and sum from two different tables
    primarykey
    data
    text
    <p>i have a problem with some querys in php and mysql: I have 2 different tables with one field in common:</p> <p>table 1</p> <p>id | hits | num_g | cats | usr_id |active</p> <p>1 | 10 | 11 | 1 | 53 | 1</p> <p>2 | 13 | 16 | 3 | 53 | 1</p> <p>1 | 10 | 22 | 1 | 22 | 1</p> <p>1 | 10 | 21 | 3 | 22 | 1</p> <p>1 | 2 | 6 | 2 | 11 | 1</p> <p>1 | 11 | 1 | 1 | 11 | 1</p> <p>table 2</p> <p>id | usr_id | points</p> <p>1 | 53 | 300</p> <hr> <p>Now i use this statement to sum just the total from the table 1 every id count + 1 too</p> <pre><code>SELECT usr_id, COUNT( id ) + SUM( num_g + hits ) AS tot_h FROM table1 WHERE usr_id!='0' GROUP BY usr_id ASC LIMIT 0 , 15 </code></pre> <p>and i get the total for each usr_id</p> <p>usr_id| tot_h |</p> <p>53 | 50</p> <p>22 | 63</p> <p>11 | 20</p> <p>until here all is ok, now i have a second table with extra points (table2) I try this:</p> <pre><code>SELECT usr_id, COUNT( id ) + SUM( num_g + hits ) + (SELECT points FROM table2 WHERE usr_id != '0' ) AS tot_h FROM table1 WHERE usr_id != '0' GROUP BY usr_id ASC LIMIT 0 , 15 </code></pre> <p>but it seems to sum the 300 extra points to all users:</p> <p>usr_id| tot_h |</p> <p>53 | 350</p> <p>22 | 363</p> <p>11 | 320</p> <p>Now how i can get the total like the first try but + the secon table in one statement? because now i have just one entry in the second table but i can be more there. thanks for all the help.</p> <hr> <p>hi thomas thanks for your reply, i think is in the right direction, but i'm getting weirds results, like </p> <p>usr_id | tot_h</p> <p>22 | NULL &lt;== i think the null its because that usr_id as no value in the table2</p> <p>53 | 1033 </p> <p>Its like the second user is getting all the the values. then i try this one: </p> <pre><code> SELECT table1.usr_id, COUNT( table1.id ) + SUM( table1.num_g + table1.hits + table2.points ) AS tot_h FROM table1 LEFT JOIN table2 ON table2.usr_id = table1.usr_id WHERE table1.usr_id != '0' AND table2.usr_id = table1.usr_id GROUP BY table1.usr_id ASC </code></pre> <p>Same result i just get the sum of all values and not by each user, i need something like this result:</p> <p>usr_id | tot_h</p> <p>53 | 53 &lt;==== plus 300 points on table1</p> <p>22 | 56 &lt;==== plus 100 points on table2</p> <p>/////////the result i need ////////////</p> <p>usr_id | tot_h</p> <p>53 | 353 &lt;==== plus 300 points on table2</p> <p>22 | 156 &lt;==== plus 100 points on table2</p> <p>I think the structure need to be something like this Pseudo statements ;)</p> <p>from table1 count all id to get the number of record where the usr_id are then sum hits + num_g and from table2 select the extra points where the usr_id are the same as table1 and get the result:</p> <p>usr_id | tot_h</p> <p>53 | 353</p> <p>22 | 156 </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload