dongpao9165 2010-11-15 14:23
浏览 145
已采纳

如何通过MySQL中的外键链接检索某些值?

sorry for the unclear title but I couldn't come up with anything better.

My dilemma is this:

I have one InnoDB table in my database called "meetings" with the following structure:

  • meeting_id (primary key, auto_increment)
  • user1_id (foreign key pointing to a user_id in a table called "users")
  • user2_id (foreign key pointing to a user_id in a table called "users")
  • time (type DATETIME)
  • location (type VARCHAR(200))

The table "users" is basic and looks like this:

  • user_id (primary key, auto_increment)
  • first_name (type VARCHAR(30))
  • last_name (type VARCHAR(30))

I have a PHP file with the aim to simply print out a description of the meeting, e.g.:

You saved the following meeting information:

User 1 | User 2 | Time & Date | Meeting location

John Doe | Jane Doe | 2010-10-10 10:10:10 | New York

Now, I simply want to use the meeting ID, call my database (only the "meetings" table) and be able to get the first_name and last_name of the user1 and user2.

Right now, my non-working code looks like this:

$query = "SELECT * FROM meetings WHERE meeting_id = " . $mid;
$data  = mysqli_query($dbc, $query);
$row   = mysqli_fetch_array($data); // ANY CHANGES HERE?

...

echo '<p>You saved the following meeting information::</p>';
echo '<table>';
echo '<tr><th>User 1</th><th>User 2</th><th>Time & Date</th><th>Meeting location</th></tr>';
echo '<td>' . $row['user1_id']['first_name'] . ' ' . $row['user1_id']['last_name'] . '</td>'; // NON-WORKING
echo '<td>' . $row['user2_id']['first_name'] . ' ' . $row['user2_id']['last_name'] . '</td>'; // NON-WORKING
echo '<td>' . $row['date_time'] . '</td>';
echo '<td>' . $row['location'] . '</td>';       
echo '</table>';
...

(How) can I retrieve the first_name/last_name links without making separate calls to the "users" table? When I check phpMyAdmin, the InnoDB foreign key links seem to work fine. Many thanks in advance!

  • 写回答

4条回答 默认 最新

  • dongtaijue1578 2010-11-15 14:30
    关注

    If your query would be

    $query = "SELECT time, location, user1.first_name, user1.last_name, user2.first_name, user2.last_name FROM meetings m JOIN users user1 ON m.user1_id = user1.id JOIN users user2 ON m.user2_id = user2.id WHERE meeting_id = " . $mid;
    

    Then the names should be available as

    $row['user1.first_name']
    

    and so on. Foreign keys are not magical devices that always bring in related records (nor should they be).

    EDIT:
    As a side note - having columns that end with numbers such as user1_id and user2_id normally raise a red flag in the mind of people who understand normalization and database design. Basically it boils down to the question - are you ready to accept that your meetings will support only meetings between two people and two people only? Another question that you should answer is: are you sure you want to distinguish between the 'first' and the 'second' participant of the meeting? (current design will make it harder to answer questions such as - list all the meetings for $user. with current table layout you will have to test both fields separately, which might hurt performance)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的