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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:45:57+00:00 2026-06-01T08:45:57+00:00

Is it possible to achieve this task with a single MySQL query? Table urls

  • 0

Is it possible to achieve this task with a single MySQL query?

Table urls. Fields {id, url}

1, http://www.mysite.kom
2, mysite.kom

3, anothersite.kom

Table logs. Fields {id, url_id, group_type – a number in the range of 1..10}

1, 1, 4
2, 1, 4
3, 2, 5
4, 2, 5

5, 3, 9

The result of the query in this example should be: 1 (mysite.com and http://www.mysite.com = 1)

THE GOAL:

Need to count all distinct urls recorded in logs table, but with a few conditions:

1) Urls with and without www. prefix, like mysite.kom and http://www.mysite.kom,
should be counted as 1 (not 2).

2) Have group_type in the range of 4..6

3) Now, any of these urls with group_type 4..6, which appear in the list of those with group_type lower than 4 – should be ignored and not counted at all.

The SQL code:

SELECT COUNT(DISTINCT TRIM(LEADING 'www.' FROM b.url))
FROM logs a
INNER JOIN urls b
ON a.url_id = b.id
WHERE (group_type BETWEEN 4 AND 6) 

----- and this condition below -----

AND TRIM(LEADING 'www.' FROM b.url)
NOT IN (
  SELECT TRIM(LEADING 'www.' FROM b.url)
  FROM logs a
  INNER JOIN urls b
  ON a.url_id = b.id
  WHERE (group_type < 4)
)

If my sql query is correct, can it be optimized (to look more compact)?

  • 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-01T08:45:58+00:00Added an answer on June 1, 2026 at 8:45 am

    Here’s one way:

    SELECT trimmed_url
      FROM ( SELECT TRIM(LEADING 'www.' FROM urls.url) AS trimmed_url,
                    MIN(logs.group_type) AS min_group_type
               FROM logs
               JOIN urls
                 ON urls.id = logs.url_id
              GROUP
                 BY trimmed_url
           ) t
     WHERE min_group_type BETWEEN 4 AND 6
    ;
    

    But only you can judge whether it looks more compact to you, and only testing can determine whether it performs better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if it's possible to achieve this using the jQuery UI Slider.
I'm not sure if this is possible what I'm trying to achieve. I want
I am not sure whether it is possible to achieve this kind of implementation
Is it possible to achieve better data transfer rate with multiple parallel TCP connections
Is it really possible to achieve grade A in yslow for all things for
I was wondering if it's possible to achieve that in CSS 2/3 (or if
What are the best resources for understanding what's possible (or not possible) to achieve
Is it possible in opengl to setup blending to achieve additive color overlays? Red
Is it possible to make AsyncTask.doInBackground synchronized - or achieve the same result in
Is the above question possible? The effect I'm trying to achieve is similar to

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.