Query: NOT matches.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Query: NOT matches.

Post by Unipus »

I have two tables, each with a "category" field. What I want to do is query these two tables and return a list of all categories that are NOT matches.

SELECT * from categories, features where categories.category != features.category

That's sort of what I thought I needed, but boy is that ever wrong. I tried a left join but it also seemed to return ALL results, not excluding the ones that match. I think I'm making this more complicated than it needs to be in my head now.
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post by Unipus »

Solved it.

SELECT categories.category FROM categories LEFT JOIN features USING (category) WHERE features.category IS null;
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

first off, ur query is wrong if i'm reading it correctly. you are telling the query to look at those field names as the actual table names.

what you should do is :

Code: Select all

$sql = "Select categories, features from <table_name> where ...";
now, as far as the "where" clause in the query, i'm kinda lost myself.

you might be able to do something like this instead of trying to do it in the query :

Code: Select all

<?php
$sql = "Select categories, features from <table_name>";
$result = mysql_query($sql);
$rows = mysql_num_rows($result);

if (!$result)
{ 
     echo 'no rows found!';
}

if ($result['categories'] != $result['features'])
{
    // do something
}
?>
hope that helps. i'm sure there is a better way, but i'm not that sure about how to do it within the actual query itself.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Just wanted to mention that SQL's != can be written as <>.
How about the following?

Code: Select all

select foo from bar where muu <> 'cow'
Edit: Miswrote the post.
Last edited by JAM on Thu Nov 13, 2003 11:22 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

JAM wrote:Just wanted to mention that SQL's != is written as <>.

Code: Select all

select foo from bar where muu <> 'cow'
In MySQL you can use `!=`.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

True, I was blindly thinking about the 'usual' way. != is not available in standard sql language, as <> is.
Post Reply