Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3996380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:16:21+00:00 2026-05-20T07:16:21+00:00

i have a problem with some querys in php and mysql: I have 2

  • 0

i have a problem with some querys in php and mysql:
I have 2 different tables with one field in common:

table 1

id | hits | num_g | cats | usr_id |active

1 | 10 | 11 | 1 | 53 | 1

2 | 13 | 16 | 3 | 53 | 1

1 | 10 | 22 | 1 | 22 | 1

1 | 10 | 21 | 3 | 22 | 1

1 | 2 | 6 | 2 | 11 | 1

1 | 11 | 1 | 1 | 11 | 1

table 2

id | usr_id | points

1 | 53 | 300


Now i use this statement to sum just the total from the table 1 every id count + 1 too

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

and i get the total for each usr_id

usr_id| tot_h |

53 | 50

22 | 63

11 | 20

until here all is ok, now i have a second table with extra points (table2)
I try this:

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

but it seems to sum the 300 extra points to all users:

usr_id| tot_h |

53 | 350

22 | 363

11 | 320

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.


hi thomas thanks for your reply, i think is in the right direction, but i’m getting weirds results, like

usr_id | tot_h

22 | NULL <== i think the null its because that usr_id as no value in the table2

53 | 1033

Its like the second user is getting all the the values. then i try this one:

      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          

Same result i just get the sum of all values and not by each user, i need something like this result:

usr_id | tot_h

53 | 53 <==== plus 300 points on table1

22 | 56 <==== plus 100 points on table2

/////////the result i need ////////////

usr_id | tot_h

53 | 353 <==== plus 300 points on table2

22 | 156 <==== plus 100 points on table2

I think the structure need to be something like this
Pseudo statements 😉

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:

usr_id | tot_h

53 | 353

22 | 156

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:16:21+00:00Added an answer on May 20, 2026 at 7:16 am

    There is nothing in your subquery which calculates extra points to correlate it to the outer Table1. So, one solution is to add that correlation:

    SELECT usr_id
        , COUNT( id ) + SUM( num_g + hits ) 
        + (SELECT points 
            FROM table2 
            WHERE table2.usr_id = table1.usr_id ) AS tot_h 
    FROM table1 
    WHERE usr_id != '0' 
    GROUP BY usr_id ASC 
    LIMIT 0 , 15
    

    Another solution would be to simply join to it directly:

    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' 
    GROUP BY table1.usr_id ASC 
    LIMIT 0 , 15
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am have some problem in updating my qotwVote1a table's Vote1a field through PHP.
I have some problem whith such mysql_query INSERT INTO table VALUES ('', CURDATE()-1) why
I'm having some problems trying to perform a query. I have two tables, one
I have some problem to get the utf-8 string from PHP using jQuery $.load()
I have some problem with MySQL Workbench in that I sometimes can't set foreign
I initially create a table using some data loaded from a php mySQL query.
So I'm using PHP/MySQL and I have a tbl_profile table with the following fields:
I have a mysql table with over 4 million of data; well the problem
I have problem thinking this trough with PHP & Mysql . I have read
I have some problem with php left join php write me a error You

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.