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 8828203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:33:10+00:00 2026-06-14T07:33:10+00:00

I have two tables that have the following structure: Table 1 : Show the

  • 0

I have two tables that have the following structure:

Table 1: Show the number of tries a player has done.

Player_id, n_tries, date

Table 2: Show the number of hits a player has done.

Player_id, n_hits, date

At the end I want to have the % of hits (tries vs hits) a player has for today and the increment/decrement of hits between today and a week ago. And I would like the results to be ordered by the % of (today) hits descendent. So, for example, if we have a player with id = 1, and a player with id = 2, with this data:

Table 1:

1, 10, "2012-10-14"
2, 13, "2012-10-14"
1, 20, "2012-10-7"
2, 15, "2012-10-7"

Table 2:

1, 5, "2012-11-14"
2, 10, "2012-11-14"
1, 0, "2012-11-7"
2, 3, "2012-11-7"

I would need to finally show this:

Player_id, % hits (today), difference of hits (today vs a week ago)

2            77%       +233%
1            50%       +100% (actually is infinite -> (5 - 0)/0 )

I have the queries to get values from both the tables:

First table – Number of tries:

SELECT player_id, date, sum(n_tries) 
FROM player_tries 
WHERE (d_date = CURDATE() OR d_date = DATE_SUB(CURDATE(), INTERVAL 7 DAY))
GROUP BY date, player_id
ORDER BY player_id, date

I do a sum(n_tries) because a player can have multiple rows of tries for a particular day.

Second table – Number of hits:

SELECT player_id, date, sum(n_hits) 
FROM player_hits
WHERE (d_date = CURDATE() OR d_date = DATE_SUB(CURDATE(), INTERVAL 7 DAY))
GROUP BY date, player_id
ORDER BY player_id, date

So, my question is: If from the first query I get a list of result that look like this:

Player_id, date,         n_tries
1         2012-11-07        20
1         2012-11-14        10
2         2012-11-07        15
2         2012-11-14        13

And from the second table I get this:

 Player_id, date,         n_hits
    1         2012-11-07        0
    1         2012-11-14        5
    2         2012-11-07        3
    2         2012-11-14        10

What would be the best way to be able to mix those results and be able to have some kind of structure that is ordered by the % hits (today).?

  • 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-06-14T07:33:11+00:00Added an answer on June 14, 2026 at 7:33 am

    Select hits from today and a week ago:

    select h1.player_id, `h1.date`,
           sum(h1.hits) as hits_today,
           sum(h2.hits) as hits_last_week
    from player_hits h1
    left join player_hits h2 on h2.player_id = h1.player_id
                                and `h2.date` = date_sub(`h1.date`, interval 7 days)
    group by h1.player_id, `h1.date`;
    

    If you only want rows when there is a corresponding entry a week before, then replace the left join with an inner join.

    The select for the tries table looks equivalent.

    First select your results into arrays with player_id as keys.

    $hits_array = array();
    $result = mysqli_query(...);
    while ($row = mysqli_fetch_assoc($result)) {
        $player = $row['player_id'];
        $hits_array[$player] = $row['hits'];
    }
    

    You do the same with the tries table.

    Now you can join the two arrays on the player_id keys:

    foreach ($hits_array as $player => $hits) {
        echo "$player " . ($hits / $tries[$key]) . "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two mysql tables that have the following structure: Table 1: ---ID---------NAME------- ---1-----
I have two tables that I am joining with the following query... select *
I have two tables that have virtually identical content and very similar structure. They
I have two tables that are related via a mapping table: keywords titles I
Suppose I have two tables that are linked (one has a foreign key to
I have two tables Product having following structure: ProductID,ProductName, IsSaleTypeA, IsSaleTypeB, IsSaleTypeC 1, AAA,
I have two tables with the following structure : userid, entrydatetime, record. tableA -
I have two tables tbl_business and business_contacts of the following structure: tbl_business --- business_id
I have two tables in mysql with the following structure: Events: id Events_artists: event_id,
I have two tables orders, and line_items with following structure: Orders (id = PK,

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.