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
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.
Link to comment
Share on other sites

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???
Link to comment
Share on other sites

(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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.