PDA

View Full Version : Query MySQL where field has a value



jc_gmk
11-29-2007, 01:43 PM
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.

boogyman
11-29-2007, 01:57 PM
you did it correctly, but remember when posting code to use the [code] tags

jc_gmk
11-29-2007, 02:05 PM
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

boogyman
11-29-2007, 02:18 PM
yes I get that... you assign the some variable to those, and if either of them is not set the query will fail.


$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



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

jc_gmk
11-29-2007, 02:42 PM
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