Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

mySQL statement problem

  • 09-11-2005 12:15AM
    #1
    Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭


    I have two tables

    table1
    id colour
    +
    +
    46 red
    54 blue
    23 blue
    28 black

    table2
    id category
    +
    +
    {Empty}

    table2 is an empty table

    I want to select all ids in table1 which are not in table2.

    How do you do this?

    I have

    select * from table1 where (colour = 'black' and (table1.id!=ANY(Select id from table2)))

    This returns empty set which is not the answer I was looking for.

    Any assistance?


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Try
    SELECT table1.* FROM table1 JOIN table2 ON table1.id != table2.id


  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    Thanks seamus but still returns an empty set.....


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    I assume you've tried teh obvious and removed the check against colour to ensure that the error/problem must be with the id comparison?

    I'm no mySQL expert, but if your colour is stored in the table as, for example, 'black ', then the query is behaving entirely correctly as it stands.

    Does MySQL (or this version thereof) support OUTER JOIN syntax? If so, what you want is :

    ... FROM table1 LEFT OUTER JOIN table2 on table1.id = table2.id WHERE ...

    jc


  • Closed Accounts Posts: 324 ✭✭madramor


    SELECT * FROM table1
    WHERE
    id NOT IN (SELECT id FROM table2);

    or older versions

    SELECT table1.id FROM table1
    LEFT JOIN table2
    ON
    table1.id=table2.id
    WHERE
    table2.id IS NULL;


  • Closed Accounts Posts: 324 ✭✭madramor


    thats straight out of the MySql docs version 5
    chapter 14.1.8


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    madramor, think thats done it,

    Thanks


Advertisement