Question

I'm trying to match a clients preferences to a house/flat. I've tried the following query below, however it seems to reject it, any ideas why?

select b.*
from propertyforrent b, client a
where a.fname = 'John'
having a.preftype = b.type and a.maxrent <= b.rent;
Was it helpful?

Solution

Use:

select b.*
  from propertyforrent b
  join client a
    on a.preftype = b.type
 where a.fname = 'John'
   and a.maxrent <= b.rent

The HAVING clause is for comparing aggregate values.

"a.preftype = b.type" is a join condition (use join clause)

"a.maxrent <= b.rent" is a horizontal comparison (not an aggregate/vertical comparison)

OTHER TIPS

You should only use having when you're also using group by (with an aggregate query). Perhaps you're looking for something more like this:

select b.*
from propertyforrent b
   join client a on a.preftype = b.type and a.maxrent <= b.rent
where a.fname = 'John'

having is used after group by, see MySQL - SELECT Syntax.

What you have here is an implicit join between two tables propertyforrent and client. You can either say

select b.*
from propertyforrent b, client a
where a.fname = 'John' and a.preftype = b.type and a.maxrent <= b.rent;

or use an explicit join

select b.*
from propertyforrent b
join client a on a.preftype = b.type
where a.fname = 'John' and and a.maxrent <= b.rent;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top