SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot Fizlar's Avatar
    Join Date
    Aug 2003
    Location
    Sheffield
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    complex sql statment

    hi i have this table


    CREATE TABLE COURSE_MENU(
    COURSE_MENU_ID INT UNSIGNED PRIMARY KEY NOT NULL DEFAULT '0' AUTO_INCREMENT,
    COURSE1_ID VARCHAR (7),
    COURSE2_ID VARCHAR (7),
    COURSE3_ID VARCHAR (7),
    COURSE4_ID VARCHAR (7),
    COURSE5_ID VARCHAR (7),
    COURSE6_ID VARCHAR (7),
    COURSE7_ID VARCHAR (7),
    ALTERNATIVE_PHRASE_ID INT UNSIGNED,
    FOREIGN KEY (COURSE1_ID) REFERENCES COURSES,
    FOREIGN KEY (COURSE2_ID) REFERENCES COURSES,
    FOREIGN KEY (COURSE3_ID) REFERENCES COURSES,
    FOREIGN KEY (COURSE4_ID) REFERENCES COURSES,
    FOREIGN KEY (COURSE5_ID) REFERENCES COURSES,
    FOREIGN KEY (COURSE6_ID) REFERENCES COURSES,
    FOREIGN KEY (COURSE7_ID) REFERENCES COURSES,
    FOREIGN KEY (ALTERNATIVE_PHRASE_ID) REFERENCES ALTERNATIVE_PHRASE);


    now this is what i want to do

    i have this alternative phrase and want to say if it matches the one in this table then i want it to show all the information

    when i type this


    SELECT * FROM COURSE_MENU, COURSES WHERE ALTERNATIVE_PHRASE_ID ='$id' AND COURSE_MENU.COURSE1_ID =COURSES.COURSE_ID AND
    COURSE_MENU.COURSE2_ID =COURSES.COURSE_ID AND
    COURSE_MENU.COURSE3_ID =COURSES.COURSE_ID AND
    COURSE_MENU.COURSE4_ID =COURSES.COURSE_ID AND
    COURSE_MENU.COURSE5_ID =COURSES.COURSE_ID AND
    COURSE_MENU.COURSE6_ID =COURSES.COURSE_ID AND
    COURSE_MENU.COURSE7_ID =COURSES.COURSE_ID;

    i get no retrieval (tried both in sql and php)

    but it should come back with all the data should it not ... i know some of the course3/4/5/6/7 will be empty but that should not matter the ones it should match should retrieve

    i tried this

    SELECT COURSE_NAME FROM COURSE_MENU, COURSES WHERE ALTERNATIVE_PHRASE_ID ='8' AND COURSE_MENU.COURSE2_ID =COURSES.COURSE_ID;


    and it worked fine but when i expanded it to say and course_menu.course2_id.=courses.course_id

    again no replies

    i have done it wrong haven't I?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,275
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    i think you need some ORs where you have ANDs
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Zealot Fizlar's Avatar
    Join Date
    Aug 2003
    Location
    Sheffield
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i thought that also... but if i use or does it not mean that it wont match all of them ie

    match this or this.... instead of match this and this and this etc which is what i want

    sorry that might be confusing but i want to show all the courses in that table and at the moment i cant?!

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,275
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    okay, i get it, you want the names of the 7 courses
    Code:
    select c1.course_name
         , c2.course_name
         , c3.course_name
         , c4.course_name
         , c5.course_name
         , c6.course_name
         , c7.course_name
      from course_menu
    left outer
      join courses c1
        on course_menu.course1_id = c1.course_id
    left outer
      join courses c2
        on course_menu.course1_id = c2.course_id
    left outer
      join courses c3
        on course_menu.course1_id = c3.course_id
    left outer
      join courses c4
        on course_menu.course1_id = c4.course_id
    left outer
      join courses c5
        on course_menu.course1_id = c5.course_id
    left outer
      join courses c6
        on course_menu.course1_id = c6.course_id
    left outer
      join courses c7
        on course_menu.course1_id = c7.course_id
     WHERE ALTERNATIVE_PHRASE_ID ='$id'
    rudy


Bookmarks

Posting Permissions

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