mysql – 使用查询从连接的表中检索数据

我想做的事:

我想检索有关客户的所有数据字段(包括与他相关的表中的所有数据(例如,引用的国家和数据字段的数据字段)

我想将我的查询作为idUser的参数.

这是我的EER图:

《mysql – 使用查询从连接的表中检索数据》

我尝试了什么:

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.*将返回某个表的每一列.

JOINS是正确的,您将表与给定的条件合并:

customer.ReferralAgent_idreferralAgent = referralagent.idReferralAgent

customer.countryResidence = country.idCountry

customer.nationality = nationality.idNationality

点赞