join two foreign keys to one primary key in other table

Posted on 2006-03-29
Last Modified: 2006-11-18
I have two tables messages and members.

id | from_member_id | to_member_id | date | subject | body
1 | 1 | 2 | 2005-03-30 05:05:055 | test | testing it

id | name
1 | sukh
2 | dukh

Now messages.from_member_id and messages.to_member_id are foreing keys pointing to same  primary key column

I want to know is it possible to get names of both members using one join.

following query cannot work I think as how dbms will know which member name is from and which is to.

select as from_member, as to_member,, messages.subject from members, messages where

Question by:Sukhwinder Singh
    LVL 2

    Expert Comment

    That should work.

    just use 'from_member' and 'to_member' when you access the returned data set.


    Author Comment

    by:Sukhwinder Singh
    Doesn't work. Returns same name twice.

    CREATE TABLE `test_members` (
      `id` tinyint(3) unsigned NOT NULL auto_increment,
      `name` varchar(30) default '',
      PRIMARY KEY  (`id`),
      UNIQUE KEY `id` (`id`)

    ) TYPE=MyISAM COMMENT='All members';

    INSERT INTO test_members (id, name) VALUES (1, 'sukh');
    INSERT INTO test_members (id, name) VALUES (2, 'dukh');

    CREATE TABLE `test_messages` (
      `id` tinyint(3) unsigned NOT NULL auto_increment,
      `from_member_id` tinyint(3) unsigned default '0',
      `to_member_id` tinyint(3) unsigned default '0',
      `subject` varchar(40) default NULL,
      `date` datetime NOT NULL default '0000-00-00 00:00:00',
      PRIMARY KEY  (`id`),
    UNIQUE KEY `id` (`id`)

    ) TYPE=MyISAM COMMENT='messages';

    INSERT INTO test_messages (id, from_member_id, to_member_id, subject, date) VALUES (1, 1, 2, 'testing', '2006-03-30 05:05:05');

    select as from_member, as to_member,, test_messages.subject from test_members, test_messages where;

    sukh,sukh,2006-03-30 05:05:05,testing
    LVL 2

    Accepted Solution

    oh yeah....ok

    select as from_member, as to_member,, test_messages.subject
    from test_members as fromMember, test_members as toMember, test_messages where
    toMember.ID = test_messages.to_member_id and fromMember.ID = test_messages.from_member_id

    that will give you both names

    Author Comment

    by:Sukhwinder Singh
    yes thanks it works.

    Will this type of join (self join?) make queries little more slow when there are 100 thousand or more messages.

    I have a small question regarding mysql primary keys. If you can answer it please.

    If is say primary key(id). does it mean that mysql will automatically create a unique index on it and I won't need to create either extra index or another unique index.

    Thank you very much.
    LVL 2

    Expert Comment

    it isn't really a matter of making it slower, it is necessary to do this type of operation.  it should be a fairly fast operation.

    Primary keys are, by definition, indexed and unique

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now