Professional Web Applications Themes

MYSQL query: AND OR? - PHP Development

This will probably sound stupid. How would you do a MYSQL query that was to evaluate if either condition A or B was meet? And if so, in what order would the result come out? Say I have select * from images where model_ID ={$_REQUEST['model_ID']} , image_ID = {$_REQUEST['image_ID']} I know I could do an XOR, which is "if either, but not both" but I don't know what "if either, use A first" would be....

  1. #1

    Default MYSQL query: AND OR?

    This will probably sound stupid.

    How would you do a MYSQL query that was to evaluate if either condition A or
    B was meet? And if so, in what order would the result come out?

    Say I have

    select * from images where model_ID ={$_REQUEST['model_ID']} , image_ID =
    {$_REQUEST['image_ID']}

    I know I could do an XOR, which is "if either, but not both" but I don't
    know what "if either, use A first" would be.

    Bonge Boo! Guest

  2. #2

    Default Re: MYSQL query: AND OR?

    Bonge Boo! wrote:
    > This will probably sound stupid.
    >
    > How would you do a MYSQL query that was to evaluate if either condition A
    > or B was meet? And if so, in what order would the result come out?
    >
    > Say I have
    >
    > select * from images where model_ID ={$_REQUEST['model_ID']} , image_ID =
    > {$_REQUEST['image_ID']}
    >
    > I know I could do an XOR, which is "if either, but not both" but I don't
    > know what "if either, use A first" would be.
    Do you mean this is what you want?

    select * from images where model_ID = 'foo' or image_ID = 'bar'

    Unless you specifically set the order using "order by" the order is
    essentially random eg you could order it "order by model_ID, image_ID"

    --
    Chris Hope - The Electric Toolbox - [url]http://www.electrictoolbox.com/[/url]
    Chris Hope Guest

  3. #3

    Default Re: MYSQL query: AND OR?

    On 7/7/04 9:57 pm, in article 1089233879_15263216.128.74.129, "Chris Hope"
    <blackholeelectrictoolbox.com> wrote:
    >> Say I have
    >>
    >> select * from images where model_ID ={$_REQUEST['model_ID']} , image_ID =
    >> {$_REQUEST['image_ID']}
    >>
    >> I know I could do an XOR, which is "if either, but not both" but I don't
    >> know what "if either, use A first" would be.
    >
    > Do you mean this is what you want?
    >
    > select * from images where model_ID = 'foo' or image_ID = 'bar'
    >
    > Unless you specifically set the order using "order by" the order is
    > essentially random eg you could order it "order by model_ID, image_ID"
    Not really. I was hoping for:

    If A is true OR b is true do something, but if b is true, then ignore the a
    part and just return b query

    I'm not explaining very well. The problem I had is that when my query is
    looking for 2 variables to be passed, if one of them isn't there, then the
    query fails with an error message.

    I was trying to have a page which says "if no value for image_ID" is set,
    then simply use the model_ID to get a result out, which will display an
    image.

    However if an explicit image_ID is set, then return this image.

    Maybe I'm doing this wrongly and should be using

    if (isset($image_ID) {

    Do this mysql query
    }else {

    Do another mysql query
    }

    Problem is I'm using Golive and its never that keen on you messing with the
    "content sources" Dreamweaver also behaves similarly AFAIK.

    Bonge Boo! Guest

  4. #4

    Default Re: MYSQL query: AND OR?

    *** Bonge Boo! wrote/escribió (Thu, 08 Jul 2004 09:04:44 +0100):
    > If A is true OR b is true do something, but if b is true, then ignore the a
    > part and just return b query
    You really want two separates queries, don't you?

    select this from table where a or b
    select that from table where not b
    > I'm not explaining very well. The problem I had is that when my query is
    > looking for 2 variables to be passed, if one of them isn't there, then the
    > query fails with an error message.
    Then use PHP to compose a custom query string.


    --
    --
    -- Álvaro G. Vicario - Burgos, Spain
    --
    Alvaro G Vicario Guest

  5. #5

    Default Re: MYSQL query: AND OR?

    On Wed, 07 Jul 2004 20:50:20 +0100, Bonge Boo! <bingbongspamcop.net>
    wrote:
    >How would you do a MYSQL query that was to evaluate if either condition A or
    >B was meet? And if so, in what order would the result come out?
    A ternary operator might be what you're looking for:

    ($a = true) ? ($do = "a and b") : ($do = "");

    [url]http://us2.php.net/language.operators[/url]


    bill Guest

  6. #6

    Default Re: MYSQL query: AND OR?

    SELECT * FROM your_table_name WHERE condition_1 AND condition_2

    "bill" <whoamiwhereami.not> wrote in message
    news:ikhte0hqdrfrsmino3hh8erhpihjqu3sgt4ax.com...
    : On Wed, 07 Jul 2004 20:50:20 +0100, Bonge Boo! <bingbongspamcop.net>
    : wrote:
    :
    : >How would you do a MYSQL query that was to evaluate if either condition A
    or
    : >B was meet? And if so, in what order would the result come out?
    :
    : A ternary operator might be what you're looking for:
    :
    : ($a = true) ? ($do = "a and b") : ($do = "");
    :
    : [url]http://us2.php.net/language.operators[/url]
    :
    :


    George Hernandez Guest

Similar Threads

  1. Is this a MySQL Query bug?
    By Carl Anatorian in forum MySQL
    Replies: 4
    Last Post: June 13th, 07:29 AM
  2. Replies: 0
    Last Post: November 4th, 05:38 PM
  3. Mysql query
    By Albert Ahtenberg in forum PHP Development
    Replies: 20
    Last Post: September 3rd, 08:51 AM
  4. MySql query help
    By luckyrye in forum PHP Development
    Replies: 2
    Last Post: May 1st, 05:25 AM
  5. [PHP] PHP - MySQL Query...
    By Jay Blanchard in forum PHP Development
    Replies: 3
    Last Post: August 14th, 11:43 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139