Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

mySQL statement problem

  • 08-11-2005 11:15pm
    #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,317 ✭✭✭✭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