MYSQL query: AND OR?

Ask a Question related to PHP Development, Design and Development.

  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. Similar Questions and Discussions

    1. Is this a MySQL Query bug?
      I wrote a SQL script in MySQL Query, and saved it as script.sql, but when I executed it in windows command line "mysql -h localhost -u root...
    2. MySQL Query Cache Not Working: MySQL 5 / Windows XP
      Please excuse this post if you've already read it on mailing.database.mysql - i just discovered these other groups. future posts will be...
    3. MySql query help
      Hey, I am trying to retrive information from a database using the 'Mysql_fetch_assoc' function, but the problem is.. it is just one row of data...
    4. Query log for mySQL?
      Is there a simple way to create a query log for mySQL? I've been asked^h^h^h^h^h told to add a layer of auditing on an existing application. It's...
    5. mysql/php query
      I have a question about (i think) joining. If I have a table in a database that has this info: key - name - favorite 1 - john - 2 2 - ...
  3. #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

  4. #3

    Default Re: MYSQL query: AND OR?

    On 7/7/04 9:57 pm, in article 1089233879_15263@216.128.74.129, "Chris Hope"
    <blackhole@electrictoolbox.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

  5. #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

  6. #5

    Default Re: MYSQL query: AND OR?

    On Wed, 07 Jul 2004 20:50:20 +0100, Bonge Boo! <bingbong@spamcop.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

  7. #6

    Default Re: MYSQL query: AND OR?

    SELECT * FROM your_table_name WHERE condition_1 AND condition_2

    "bill" <whoami@whereami.not> wrote in message
    news:ikhte0hqdrfrsmino3hh8erhpihjqu3sgt@4ax.com...
    : On Wed, 07 Jul 2004 20:50:20 +0100, Bonge Boo! <bingbong@spamcop.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

Posting Permissions

  • You may not post new threads
  • You may 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