MySQL了解基本联接 [英] MySQL Understanding Basic Joins
问题描述
嘿,我真的很难理解基本的MySQL联接. 基本上,我有2个表,一个表中有客户的名字和地址ID,另一个表中有实际地址.
Hey guys I'm really struggling to understand basic MySQL joins. Basically I've got 2 tables one with a customer first name and address id in it and another with the actual address.
我不仅要显示客户名称和地址ID,还希望它显示客户名称和实际地址.
Rather than just displaying the customer name and an address id I want it to display the customer name and the actual address.
我的基本选择语句是这样的:
My basic select statement is like this :
SELECT firstName, addressId FROM Customer
它将显示如下内容:
firstName addressId
---------------------
Bob 56
而不是我不想将addressId与另一个表中的实际地址连接起来
Rather than than I would like to join the addressId with an actual address in another table
赞:
firstName address
----------------------------------
Bob 45 Somewhere street
有没有人可以向我展示实现这一目标的最佳方法?
Is there anyone who can show me the best way to achieve this?
任何人都可以推荐一个不错的联接教程吗?
Also can anyone recommend a good tutorial for joins?
推荐答案
您的条件是内部联接, 这是最简单,最容易理解的Join,也是最常见的.该查询将返回左表(客户)中所有在右表(地址)中具有匹配记录的记录.此Join的编写方式如下:
Your condition is inner join, This is the simplest, most understood Join and is the most common. This query will return all of the records in the left table (Customer) that have a matching record in the right table (address). This Join is written as follows:
SELECT firstName, address FROM Customer
INNER JOIN address ON Customer.addressId=address.addressId
样本输出:
FIRSTNAME ADDRESS
Bob 45 Somewhere street
这篇关于MySQL了解基本联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!