Galeo
Galeo

Reputation: 3

Use a calculation inside the where part

I have a problem formulating a sql query. The code is used in a php script accessing a sqlite database.

What I want to do: I only want to select datasets which are over an individual offset:

select * from tab1 t, tab2 z where t.id = z.ref and (t.value - z.offset > '50')

Why doesn't this work?

I tried another way but this fails too.

select *, t.value - z.offset as diff from tab1 t, tab2 z where t.id = z.ref and ( diff > '50')

The colum diff contains the correct value, but I cannot use it in the where clause.

Any idea how to formulate this? Thanks for your help! Galeo

Upvotes: 0

Views: 34

Answers (1)

PM 77-1
PM 77-1

Reputation: 13334

If t.value and z.offset are both of some numeric type, then re-write your last condition as (t.value - z.offset > 50) (no apostrophes).

Your 2nd query uses column alias in WHERE clause which is not allowed.

Upvotes: 1

Related Questions