Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Queries return contrary results (Postgre SQL)

Queries return contrary results (Postgre SQL)

Scheduled Pinned Locked Moved Database
databasequestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lutoslaw
    wrote on last edited by
    #1

    Hello, How is this possible? "orders" has an FK ao_campaign_id to "ao_campaigns".

    =# select id from ao_campaigns where id=2851;
    id

    2851
    (1 row)

    =# select id from orders where ao_campaign_id=2851;
    id

    (0 rows)

    =# select * from ao_campaigns where id not in (select ao_campaign_id from orders);
    id | id_tvn | order_id | start_date | end_date | label
    ----+--------+----------+------------+----------+-------
    (0 rows)

    (types are correct, all ids are integers). The last query should return at least one result - 2851, right? What am I missing? Greetings, Jacek

    L 1 Reply Last reply
    0
    • L Lutoslaw

      Hello, How is this possible? "orders" has an FK ao_campaign_id to "ao_campaigns".

      =# select id from ao_campaigns where id=2851;
      id

      2851
      (1 row)

      =# select id from orders where ao_campaign_id=2851;
      id

      (0 rows)

      =# select * from ao_campaigns where id not in (select ao_campaign_id from orders);
      id | id_tvn | order_id | start_date | end_date | label
      ----+--------+----------+------------+----------+-------
      (0 rows)

      (types are correct, all ids are integers). The last query should return at least one result - 2851, right? What am I missing? Greetings, Jacek

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #2

      Got it! Correct query:

      select * from ao_campaigns where ao_campaigns.id not in (select orders.ao_campaign_id from orders where ao_campaign_id IS NOT NULL);

      Some of ao_campaign_id in orders were NULL, which are treated as "unknown" and therefore uncomparable. It is not intuitive in this particular case, though. Reference: sql null logic - Szukaj w Google[^]

      J 1 Reply Last reply
      0
      • L Lutoslaw

        Got it! Correct query:

        select * from ao_campaigns where ao_campaigns.id not in (select orders.ao_campaign_id from orders where ao_campaign_id IS NOT NULL);

        Some of ao_campaign_id in orders were NULL, which are treated as "unknown" and therefore uncomparable. It is not intuitive in this particular case, though. Reference: sql null logic - Szukaj w Google[^]

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        Or get used to use EXISTS instead of IN. EXISTS does not use three valued logic.

        SELECT *
        FROM ao_campaigns
        WHERE NOT EXISTS (
        SELECT *
        FROM orders
        WHERE orders.ao_campaign_id = ao_campaigns.id
        )

        Wrong is evil and must be defeated. - Jeff Ello

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups