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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:51:36+00:00 2026-05-26T23:51:36+00:00

My query is: SELECT * FROM sites WHERE status = ‘1’ ORDER BY id

  • 0

My query is:

SELECT * FROM sites WHERE status = ‘1’ ORDER BY id DESC

From here, inside my while loop I have the all the urls that are active. I want to check these urls in another table 'hits‘, and get the SUM of the ‘stats’ for each site that is active.

How can this be accomplished? Is this done with a JOIN statement?

table structures

sites

id  int(11) unsigned    NO  PRI NULL    auto_increment
url varchar(100)    NO  UNI     
status  int(11) YES     1   
added   timestamp   NO      CURRENT_TIMESTAMP   on update CURRENT_TIMESTAMP

hits

id  int(11) unsigned    NO  PRI NULL    auto_increment
domain  varchar(30) NO          
stats   int(11) YES     NULL    
added   timestamp   NO      CURRENT_TIMESTAMP   
  • 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-26T23:51:37+00:00Added an answer on May 26, 2026 at 11:51 pm
    SELECT
        A.id,COUNT(1) site_hits
    FROM
        (SELECT id FROM sites WHERE status='1' ORDER BY id DESC) A
        INNER JOIN
        hits B ON A.id = B.site_id
    GROUP BY
        A.id
    ;
    

    CAVEAT #1 for this query : You will need a good index for this one. I recommend the following:

    ALTER TABLE sites ADD INDEX status_id_ndx (status,id);
    

    CAVEAT #2 for this query : You could make the index more efficient.

    With status being int(11), you could accommodate 2147483647 possible status values. I don’t think you have that many. If the highest value for status < 256, you could change it as follows:

    ALTER TABLE sites MODIFY COLUMN status int unsigned not null;
    

    Result? Smaller table, small index, and faster access.

    You need something like this. I do not see any correlation between sites and hits in your question. You need the id of the site to be stored in hits table. If you are trying to join by domain against url, you will need to express the join that way. That part would seem messy. It would resemble something like this:

    SELECT
        A.id,COUNT(1) site_hits
    FROM
        sites A,hits B
    WHERE
        LOCATE(B.domain,A.url)
    GROUP BY
        A.id
    ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query to select from another sub-query select. While the two queries
I have this query: SELECT p.id, r.status, r.title FROM page AS p INNER JOIN
I have the following query: SELECT DISTINCT sites.site_id, sites.site_name, sites.site_url, earnings.cust_id FROM sites, earnings
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
Take this query: SELECT * FROM MyTable WHERE MyColumn = 'SomeValue' ORDER BY SomeFakeQualifier.MyColumn
$result = mysql_query(SELECT * FROM project ORDER BY projectid); while($row = mysql_fetch_array($result)) { return(array($row['projectid'],
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have this query: SELECT * FROM sample INNER JOIN test ON sample.sample_number =
$query = mysql_query(SELECT * FROM news WHERE id = '{$_GET['id']}'); $news = mysql_fetch_assoc($query); $sql84
OdbcDataReader q = dbc.Query(SELECT * FROM `posts` WHERE `id`= + id.ToString()); if (q.RecordsAffected <

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.