Results 1 to 5 of 5

Thread: Query MySQL where field has a value

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Query MySQL where field has a value

    I need help writing a query

    I'm trying to write this as a proper query

    SELECT * FROM products WHERE model='has a value' AND type='has a value' GROUP BY brand

    If neither model or type have a value then I don't want to return the brand.
    Hope this makes sense.

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    you did it correctly, but remember when posting code to use the [code] tags

  3. #3
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    No what I meant was:

    I have thousands of entries in my database and only want to return the brand field (grouped) if the fields 'model' and 'type' have any value in
    not litterally 'has a value' but if it just contains some text other than null

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    yes I get that... you assign the some variable to those, and if either of them is not set the query will fail.
    PHP Code:
    $query "SELECT * FROM products WHERE model = '"$model ."' AND type = '"$type ."' GROUP BY brand"
    provided you are doing this in PHP

    or you could do this

    PHP Code:
    if( !is_null($model) && !is_null($type) )
    {
         
    $query "SELECT * FROM products WHERE model = '"$model ."' AND type = '"$type ."' GROUP BY brand";


  5. #5
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    That won't do what i'm trying to do

    if my table looks something like this:

    +-----------+---------+------------------+
    | BRAND | MODEL | TYPE |
    +-----------+---------+------------------+
    | brand1 | model1 | type1 |
    +-----------+---------+------------------+
    | brand1 | model8 | type3 |
    +-----------+---------+------------------+
    | brand2 | model4 | |
    +-----------+---------+------------------+
    | brand3 | model9 | type6 |
    +-----------+---------+------------------+

    The only values i would want returned are:
    brand1 and brand3
    as brand2 has not got an entry for type

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
  •