Thread: Complex SELECT Statement help needed

Complex SELECT Statement help needed

From
flashbangpop
Date:
I'm new to postgres and am trying to display rows in a table that are have a
value between 2 fields.

The rows I want to display should be selected by the grademin or grademax
fields. I'd like to display the rows in sections that are labeled Grades
3-5, 6-8, and 9-12.

The problem that I am having is I can't figure out how to write a statement
that will check both fields. And, especially when I have values in the like
this:

grademin 4 grademax 12 that I want to show up in all levels
grademin 5 grademax 12 ""
grademin 6 grademax 9 in 6-9 and 9-12

Below is what I have so far, but it falls flat on it's face when trying to
display rows that don't fall between the specific range.

How can I check grademin and max in the same statement?


$query = "SELECT
lessonwebid,lessontitle,gradelevel,grademin,grademax,reviewedby FROM
lessonplans WHERE grademin >= 3 OR grademin <= 5";

$query = "SELECT
lessonwebid,lessontitle,gradelevel,grademin,grademax,reviewedby FROM
lessonplans WHERE grademin >= 6 OR grademin <= 8";

$query = "SELECT
lessonwebid,lessontitle,gradelevel,grademin,grademax,reviewedby FROM
lessonplans WHERE grademin >= 9 OR grademin <= 12";

Thanks ahead for any suggestions.
--
View this message in context: http://old.nabble.com/Complex-SELECT-Statement-help-needed-tp27691760p27691760.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: Complex SELECT Statement help needed

From
Raymond O'Donnell
Date:
On 22/02/2010 18:14, flashbangpop wrote:

> I'm new to postgres and am trying to display rows in a table that are have a
> value between 2 fields.
>
> The rows I want to display should be selected by the grademin or grademax
> fields. I'd like to display the rows in sections that are labeled Grades
> 3-5, 6-8, and 9-12.
>
> The problem that I am having is I can't figure out how to write a statement
> that will check both fields. And, especially when I have values in the like
> this:
>
> grademin 4 grademax 12 that I want to show up in all levels
> grademin 5 grademax 12 ""
> grademin 6 grademax 9 in 6-9 and 9-12
>
> Below is what I have so far, but it falls flat on it's face when trying to
> display rows that don't fall between the specific range.
>
> How can I check grademin and max in the same statement?

Are grademin and grademax two separate columns in the table? If so,
simply imcude as many conditions as you need:

  select ....
  where grademin ...
  and grademax ....
  and ....


Ray.


--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

Re: Complex SELECT Statement help needed

From
flashbangpop
Date:
Thanks, I got these working:

$query = "SELECT
lessonwebid,lessontitle,gradelevel,grademin,grademax,reviewedby FROM
lessonplans WHERE grademin >= 3 AND grademin <= 5 OR grademax >= 3 AND
grademax <= 5";




Raymond O'Donnell wrote:
>
> On 22/02/2010 18:14, flashbangpop wrote:
>
>> I'm new to postgres and am trying to display rows in a table that are
>> have a
>> value between 2 fields.
>>
>> The rows I want to display should be selected by the grademin or grademax
>> fields. I'd like to display the rows in sections that are labeled Grades
>> 3-5, 6-8, and 9-12.
>>
>> The problem that I am having is I can't figure out how to write a
>> statement
>> that will check both fields. And, especially when I have values in the
>> like
>> this:
>>
>> grademin 4 grademax 12 that I want to show up in all levels
>> grademin 5 grademax 12 ""
>> grademin 6 grademax 9 in 6-9 and 9-12
>>
>> Below is what I have so far, but it falls flat on it's face when trying
>> to
>> display rows that don't fall between the specific range.
>>
>> How can I check grademin and max in the same statement?
>
> Are grademin and grademax two separate columns in the table? If so,
> simply imcude as many conditions as you need:
>
>   select ....
>   where grademin ...
>   and grademax ....
>   and ....
>
>
> Ray.
>
>
> --
> Raymond O'Donnell :: Galway :: Ireland
> rod@iol.ie
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>
>

--
View this message in context: http://old.nabble.com/Complex-SELECT-Statement-help-needed-tp27691760p27692084.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.