Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

MySQL   how to use calculation in WHERE clause

Posted on 2011-03-17
6
343 Views
Last Modified: 2012-05-11
i initally tried to do
SELECT     (long_equation_here_that_uses_fields_in_this_row) AS Mileage ...  
      ...   WHERE   Mileage < TblFieldMileage  ...

in order to select only the Rows (records)  where the calculated Mileage was less than the table's field content value for that row.

How can i accomplish this on the fly calculation PER ROW in order to use in a relational expression in the WHERE clause?
0
Comment
Question by:willsherwood
  • 2
  • 2
6 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 250 total points
ID: 35156995
Small adjustment.
You can use it in the where:

SELECT  *  from table1
WHERE TblFieldMileage > (long_equation_here_that_uses_fields_in_this_row)
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 250 total points
ID: 35157003
you cannot use the alias in the WHERE, only in the ORDER BY.
so, you have to repeat the expression....

unless you put a subquery:

SELECT *
  FROM ( 
SELECT   TblFieldMileage  ,   (long_equation_here_that_uses_fields_in_this_row) AS Mileage 
    FROM ...
  WHERE ...   
 ) as sub_query
WHER Mileage < TblFieldMileage  ...

Open in new window

0
 

Author Comment

by:willsherwood
ID: 35157063
Thanks Angel, that's even better!   I did not know about subqueries.
Wish i could give you points, sorry for the timing overlap.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35157273
willsherwood,
You can request the moderator attention, and ask for the redistribution of points, as you prefer.
0
 

Author Comment

by:willsherwood
ID: 35173433
i requested attn 3/17  and no response, so i'm trying again now 3/19
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question