mysql Table Select

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elpetha Brooke

    #1

    mysql Table Select

    Hi

    I have two tables

    TABLE1
    t1_id int not null
    t1_other stuff blah blah

    TABLE2
    t2_id int not null
    t2_t1_id int not null
    t2_other stuff blah blah

    the link between the tables is t1_id --> t2_t1_id

    There is a list of items with ID 1-10 in TABLE1
    in TABLE2 there is a list of items with the t2_t1_id 1-5

    What I want to do is select stuff from TABLE1 because they don't have an
    entry in TABLE2 i.e. 6-10

    Can this be done?

    Thanks in advance

    Eurisko


  • Wouter

    #2
    Re: mysql Table Select

    Yes,

    your tables both needs to be of the InnoDB type (not MYISAM !),
    and you need to use:

    ALTER TABLE `table2`ADD CONSTRAINT `table2_ibfk1` FOREIGN KEY (`td2_id`)
    REFERENCES `table1` (`td1_id`) ON UPDATE CASCADE;


    Or someting alike.
    Look for documentation about FOREIGN KEY
    (in google search for: foreign key mysql )

    Ciao,
    Wouter




    "Elpetha Brooke" <nsutton1978@nt lworld.com> wrote in message
    news:Bw6kd.107$ ha2.45@newsfe6-win.ntli.net...
    : Hi
    :
    : I have two tables
    :
    : TABLE1
    : t1_id int not null
    : t1_other stuff blah blah
    :
    : TABLE2
    : t2_id int not null
    : t2_t1_id int not null
    : t2_other stuff blah blah
    :
    : the link between the tables is t1_id --> t2_t1_id
    :
    : There is a list of items with ID 1-10 in TABLE1
    : in TABLE2 there is a list of items with the t2_t1_id 1-5
    :
    : What I want to do is select stuff from TABLE1 because they don't have an
    : entry in TABLE2 i.e. 6-10
    :
    : Can this be done?
    :
    : Thanks in advance
    :
    : Eurisko
    :
    :


    Comment

    • Elpetha Brooke

      #3
      Re: mysql Table Select

      Thanks Wouter, but what sql statement would acheive my goal

      Thanks again

      Eurisko


      "Wouter" <nospam_nopriva temail@djwice.c om> wrote in message
      news:cmqvis$nbc $1@news.tudelft .nl...[color=blue]
      > Yes,
      >
      > your tables both needs to be of the InnoDB type (not MYISAM !),
      > and you need to use:
      >
      > ALTER TABLE `table2`ADD CONSTRAINT `table2_ibfk1` FOREIGN KEY (`td2_id`)
      > REFERENCES `table1` (`td1_id`) ON UPDATE CASCADE;
      >
      >
      > Or someting alike.
      > Look for documentation about FOREIGN KEY
      > (in google search for: foreign key mysql )
      >
      > Ciao,
      > Wouter
      >
      >
      >
      >
      > "Elpetha Brooke" <nsutton1978@nt lworld.com> wrote in message
      > news:Bw6kd.107$ ha2.45@newsfe6-win.ntli.net...
      > : Hi
      > :
      > : I have two tables
      > :
      > : TABLE1
      > : t1_id int not null
      > : t1_other stuff blah blah
      > :
      > : TABLE2
      > : t2_id int not null
      > : t2_t1_id int not null
      > : t2_other stuff blah blah
      > :
      > : the link between the tables is t1_id --> t2_t1_id
      > :
      > : There is a list of items with ID 1-10 in TABLE1
      > : in TABLE2 there is a list of items with the t2_t1_id 1-5
      > :
      > : What I want to do is select stuff from TABLE1 because they don't have an
      > : entry in TABLE2 i.e. 6-10
      > :
      > : Can this be done?
      > :
      > : Thanks in advance
      > :
      > : Eurisko
      > :
      > :
      >
      >[/color]


      Comment

      • Bill Karwin

        #4
        Re: mysql Table Select

        Elpetha Brooke wrote:[color=blue]
        > What I want to do is select stuff from TABLE1 because they don't have an
        > entry in TABLE2 i.e. 6-10[/color]

        SELECT TABLE1.*
        FROM TABLE1 LEFT OUTER JOIN TABLE2 ON TABLE1.t1_id = TABLE2.t2_t1_id
        WHERE TABLE2.t2_t1_id IS NULL

        Regards,
        Bill K.

        Comment

        • Eurisko

          #5
          Re: mysql Table Select

          Thanks Bill That did the trick ;)

          Eurisko


          "Bill Karwin" <bill@karwin.co m> wrote in message
          news:cmr7fc02m4 r@enews1.newsgu y.com...[color=blue]
          > Elpetha Brooke wrote:[color=green]
          > > What I want to do is select stuff from TABLE1 because they don't have an
          > > entry in TABLE2 i.e. 6-10[/color]
          >
          > SELECT TABLE1.*
          > FROM TABLE1 LEFT OUTER JOIN TABLE2 ON TABLE1.t1_id = TABLE2.t2_t1_id
          > WHERE TABLE2.t2_t1_id IS NULL
          >
          > Regards,
          > Bill K.[/color]


          Comment

          • Phillip T. Murphy

            #6
            Re: mysql Table Select


            "Wouter" <nospam_nopriva temail@djwice.c om> wrote in message
            news:cmqvis$nbc $1@news.tudelft .nl...[color=blue]
            > Yes,
            >
            > your tables both needs to be of the InnoDB type (not MYISAM !),
            > and you need to use:
            >
            > ALTER TABLE `table2`ADD CONSTRAINT `table2_ibfk1` FOREIGN KEY (`td2_id`)
            > REFERENCES `table1` (`td1_id`) ON UPDATE CASCADE;
            >
            >
            > Or someting alike.
            > Look for documentation about FOREIGN KEY
            > (in google search for: foreign key mysql )
            >
            > Ciao,
            > Wouter
            >
            >[/color]
            Why would he need to change is table type to InnoDB? Why not just use an
            outer join?


            Comment

            • Wouter

              #7
              Re: mysql Table Select

              Yes, I was to quick in my responds.

              The join that Bill did suggest is the way to do it.

              I only read;

              I want table1_id -> table2_id

              and so wrote the alter table responds, way to quick ;)

              Wouter


              "Phillip T. Murphy" <pStPmAuMrphy@b ellsouth.net> wrote in message
              news:AVpkd.5305 $z3.2276@bignew s5.bellsouth.ne t...
              :
              : "Wouter" <nospam_nopriva temail@djwice.c om> wrote in message
              : news:cmqvis$nbc $1@news.tudelft .nl...
              : > Yes,
              : >
              : > your tables both needs to be of the InnoDB type (not MYISAM !),
              : > and you need to use:
              : >
              : > ALTER TABLE `table2`ADD CONSTRAINT `table2_ibfk1` FOREIGN KEY (`td2_id`)
              : > REFERENCES `table1` (`td1_id`) ON UPDATE CASCADE;
              : >
              : >
              : > Or someting alike.
              : > Look for documentation about FOREIGN KEY
              : > (in google search for: foreign key mysql )
              : >
              : > Ciao,
              : > Wouter
              : >
              : >
              : Why would he need to change is table type to InnoDB? Why not just use an
              : outer join?
              :
              :


              Comment

              Working...