我想做的是:
我想检索关于aCustomer
的所有数据字段(包括与他相关的表中的所有数据(例如Country
的数据字段和referralagent
的数据字段)
我希望我的查询将idUser
作为参数。
这是我的eer图:
我试过的:
SELECT
referralagent.firstName
,referralagent.lastName
,customer.firstName
,customer.lastName
,customer.address
,customer.postcode
,customer.profession
,customer.phoneNumWork
,customer.phoneNumMobile
,customer.phoneNumHome
,country.name
,nationality.name
FROM
customer
INNER JOIN referralagent
ON customer.ReferralAgent_idreferralAgent = referralagent.idReferralAgent
INNER JOIN country
ON customer.countryResidence = country.idCountry
INNER JOIN nationality
ON customer.nationality = nationality.idNationality
WHERE customer.idUser = '7'
问题是:
我不相信我所尝试的是“正确的”。有
有更好的办法吗?
最佳答案
您的查询基本上是正确的,我刚刚编辑了SELECT
部分,所以它要短一些。
SELECT customer.*, referralagent.firstName,
referralagent.lastName, country.name, nationality.name
FROM customer, country, nationality, referralagent
INNER JOIN referralagent
ON customer.ReferralAgent_idreferralAgent = referralagent.idReferralAgent
INNER JOIN country
ON customer.countryResidence = country.idCountry
INNER JOIN nationality
ON customer.nationality = nationality.idNationality
只键入
*
将返回每一列。输入table_name.*
返回某个表的每一列。联接是正确的将表与给定条件合并:
customer.ReferralAgent_idreferralAgent = referralagent.idReferralAgent
customer.countryResidence = country.idCountry
customer.nationality = nationality.idNationality