Jump to content

How to get exceptions to a query?


Pasqualz

Recommended Posts

Hi All,

I recently posted a question here [url=http://www.phpfreaks.com/forums/index.php/topic,122311.0.html]http://www.phpfreaks.com/forums/index.php/topic,122311.0.html[/url] and I now realize that it brings up an interesting question. How do you get the exceptions to a query? In other words, if you have a simple table:

Name, class, grade
John, history, 91
Sally, math , 75
Steve, english, 80
Joe, science, 95

It's easy to query once and get all the rows where grade > 90, and it's eaqually easy to query a second time and get all the rows where grade < 90, but is it possible to instead, query all rows where grade > 90, then tell the query to give you the exceptions, which would produce the results of the second query. Just curious?
Link to comment
https://forums.phpfreaks.com/topic/34239-how-to-get-exceptions-to-a-query/
Share on other sites

I updated my previous post; "id" was left over from my copy/paste.

See IF in [url=http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html]Control Flow Functions[/url]. AS simply creates an alias--without it your column name would literally be "IF(grade >= 90, 1, 0)" instead of a cleaner, clearer "90plus."

The query selects all the records, adding a new column to the ouput called "90plus." If a record's grade is greater than or equal to 90, it will be 1; if not, it will be 0.
Effigy, thanks so much. I had no idea you could put conditional statements into a query! I have one other question, the query as structured, returns every row with the aliased '90plus' column. Is there a way to get the query to return [u]only[/u] rows where the aliased '90plus' column =1???
(My)SQL has a lot of capabilities: keep reading the docs and the forums :)

If you still want to work with both result sets, use PHP to separate them: create a loop which only echos the row if 90plus is equal to 1, for instance. Otherwise, if you only want to work with one set of data, you're back where you were at square one:

[quote author=Pasqualz link=topic=122460.msg505097#msg505097 date=1168861889]
It's easy to query once and get all the rows where grade > 90, and it's eaqually easy to query a second time and get all the rows where grade < 90
[/quote]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.