Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    checking if a record exists in a table ?

    Hi,

    Simple question,
    but I have never had to do this ....

    I have two tables

    Table 1: PAGES
    Table 2: MAIN

    PAGEShas records: page_id, text

    MAIN has records: item_id, page_no, stuff

    Now ALL the page_no's on MAIN refer to page_id's of records on PAGES

    But not all pages on PAGES are on the MAIN table.

    So how do I check for records that are NOT on MAIN ??

    Would it be this ?

    PHP Code:
    $sql "SELECT pages.* FROM pages,main WHERE pages.page_id != main.page_no "

    Seems wrong somehow

    Instead of "not equal" I really want it to say "not existing" on main

    Thanks for any help.


    .
    Last edited by jeddi; 08-21-2013 at 07:19 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,765
    Thanks
    23
    Thanked 548 Times in 547 Posts
    SELECT * FROM pages
    LEFT OUTER JOIN main
    ON pages.page_id = main.page_no
    WHERE main.item_id IS null

    Gives your everything in pages that is not in main.
    Evolution - The non-random survival of random variants.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by sunfighter View Post
    SELECT * FROM pages
    LEFT OUTER JOIN main
    ON pages.page_id = main.page_no
    WHERE main.item_id IS null

    Gives your everything in pages that is not in main.
    ALMOST.

    By doing SELECT * there, you will be getting all the fields from *BOTH TABLES*. But since you only want records where main.item_id IS NULL, that means you will get all NULL fields for the values from every MAIN field.

    At a minimum, do SELECT pages.* rather than just SELECT *. Better, of course, is to specify only and exactly the fields you need.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    sunfighter (08-22-2013)

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK - Thanks

    I will try that
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •