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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:28:58+00:00 2026-06-15T03:28:58+00:00

Is there some way to have a formula that is in both the select

  • 0

Is there some way to have a formula that is in both the select and where condition only be calculated once?

I assume mysql must do the calculation twice for each row it the following example.

SELECT z1.id sid1,z2.id sid2,SQRT(POW(ABS(z1.col-z2.col),2) + POW(ABS(z1.row-z2.row),2)) r
    FROM stars z1,stars z2
    WHERE z1.id!=z2.id
    AND SQRT(POW(ABS(z1.col-z2.col),2) + POW(ABS(z1.row-z2.row),2)) <=32
    ORDER BY z1.id,z2.id
  • 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-15T03:29:00+00:00Added an answer on June 15, 2026 at 3:29 am

    You cannot refer to aliases defined in a SELECT clause in a WHERE clause, but you can use them in a HAVING clause. This is because WHERE is evaluated before SELECT as SELECT may contain aggregate functions.

    SELECT z1.id sid1,z2.id sid2,SQRT(POW(ABS(z1.col-z2.col),2) + POW(ABS(z1.row-z2.row),2)) r
     FROM stars z1,stars z2
     WHERE z1.id != z2.id
     HAVING r <= 32
     ORDER BY z1.id,z2.id
    

    Beware, though, that the HAVING clause will most likely not be used in indexes. You could provide an approximate term to the WHERE clause for indexing purposes:

    SELECT z1.id sid1,z2.id sid2,SQRT(POW(ABS(z1.col-z2.col),2) + POW(ABS(z1.row-z2.row),2)) r
     FROM stars z1,stars z2
     WHERE z1.id != z2.id
     AND (z2.col BETWEEN z1.col-32 AND z1.col+32)
     AND (z2.row BETWEEN z1.row-32 AND z1.row+32)
     HAVING r <= 32
     ORDER BY z1.id,z2.id
    

    Note that the bottleneck is disk access, not arithmetic calculation, so if you are optimising for speed, you may want to stay with WHERE and only optimise the WHERE clause:

    note that

    • x^2 is the same as abs(x)^2.
    • x*x is likely faster than pow(x,2)
    • instead of computing the sqrt, you can square the other side.

    Try:

    SELECT z1.id sid1,z2.id sid2,
      SQRT((z1.col-z2.col)*(z1.col-z2.col) + (z1.row-z2.row)*(z1.row-z2.row)) r
     FROM stars z1,stars z2
     WHERE z1.id != z2.id
     AND (z1.col-z2.col)*(z1.col-z2.col) + (z1.row-z2.row)*(z1.row-z2.row) <= 1024
     ORDER BY z1.id,z2.id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there some way when sending this message to specify that I rather have
I have a 1gig access database, is there some sort of formula that gives
Is there some way to replicate rails' link-to-unless-current? Ie. if i have a list
I have a question about SqlDataReader: Is there some way to modificated the values
I have a Type, a String and an Object. Is there some way I
I am wondering if there is some way to make an box have the
Is there a way to delay css from firing using jquery? I have some
I have some VirtualBox VMs running. Is there any way to programatically get the
I have some code to read from a pdf file. Is there a way
If we have some branch, B1, is there a way to create branch B2

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.