MySQL联接两个表ID以产生从一个表到另一个表的结果 [英] MySQL Joining Two Table ID's to Produce Results from One Table to Another

查看:66
本文介绍了MySQL联接两个表ID以产生从一个表到另一个表的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很多示例/关于stackoverflow的问题,但是我似乎无法完成我所需要的.

I've tried a whole slew of examples / answered questions on stackoverflow but I can't seem to accomplish what I need.

我有一个PHP/MySQL工具正在研究自己的项目.我认为立即优化最大的表是个好主意,因此我选择将ID的名称(而不是全名)存储在Project表中.通过PHP查看结果时,我希望将ID从Project表链接到另一个表中的实际Name,但是当然,我希望实际数据库列仅包含ID.我相信这是可能的,但我只是想不出如何将其应用于我的情况.

I have a PHP / MySQL tool I'm working on to track projects for myself. I thought it'd be a good idea to optimize the largest table right off the hop, so I chose to store ID's in the Project table as opposed to full names. I'm looking to link that ID from the Project table to the actual Name in another table when viewing results via PHP, but of course I want the actual database column to just contain the ID. I believe this is possible, I just can't figure out how to apply it to my situation.

表格-项目

ID | CustomerID | Summary  | Phase
---------------------------------
1  | 61         | Project1 | 3
2  | 42         | Project2 | 3
3  | 33         | Project3 | 5

表-客户

ID   | CustomerName
--------------------
61   | Customer 1
42   | Customer 2
33   | Customer 3

现在,当我从项目"表中调用"CustomerID"行时,它会按预期显示CustomerID号.我希望它改为从客户"表中提取客户名",将"Customers.ID"列与"Projects.CustomerID"列匹配.

Right now when I call the "CustomerID" row from the Projects Table, it displays the CustomerID number as expected. I would like it to pull the CustomerName from the Customers table instead, matching the Customers.ID column to the Projects.CustomerID column.

这可能吗?谁能指出我正确的方向?

Is this possible? Can anyone point me in the right direction?

感谢进阶!

推荐答案

您可以尝试执行以下操作:

You can try something like this:

SELECT CustomerName FROM Projects
JOIN Customers ON Customers.ID = Projects.CustomerID
WHERE Projects.ID = "1"

这篇关于MySQL联接两个表ID以产生从一个表到另一个表的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆