Results 1 to 3 of 3

Thread: SQL Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    1

    Unhappy

    HI folks,

    I have created a set of 6 tables, the three i am having a problem with are made up of a many-to-many relationship.

    Table 1 = Article
    Table 2 = Subject
    Table 3 = Article/Subject

    What i am trying to do is to output a list of all article names (Article) which are associated with a one specific subject held in (Article/Subject).

    e.g.

    A S
    1, 2, 3 1
    2, 4 2


    Subject 1 thus has article 1, 2 and 3
    Subject 2 has article 2, 4

    I can manage to list the Article ID's from the (Article/Subject) table, but unable to join the Article table to produce the names(Article.ArticleNames).

    Is there an SQL query which is going to help me get pass this point? Please advise

    ADT

  2. #2
    Hyperactive Member
    Join Date
    Apr 2000
    Location
    Isle of Man
    Posts
    276
    I don't want to sound patronising, but....

    is there a reason for having many to many relationships, because if not you would be best redesigning your tables so you have one to many relationships instead.

  3. #3
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    Hi addisss

    You haven't actually got a many to many relationship but two one to many relation's. By having the the third table you have got rid of the many problem. for one article in the article table you can have many in the joint table and for one subject in the the subject table you can have many in the joint table. To return the required result set use the following SQL.

    Code:
    SELECT sub.idcolumn, art.column2, art.column3  FROM article as art subject as sub, subject/article as subart
    WHERE sub.idcolumn = subart.subjectcode AND  subart.articlecode = art.articlecode AND sub.idcolumn = subject value
    This should sort out your problem

    Hope it helps
    Ian
    Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!

Posting Permissions

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



Click Here to Expand Forum to Full Width