我想做的是:
我想检索关于aCustomer的所有数据字段(包括与他相关的表中的所有数据(例如Country的数据字段和referralagent的数据字段)
我希望我的查询将idUser作为参数。
这是我的eer图:
mysql - 使用查询从连接的表中检索数据-LMLPHP
我试过的:

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

09-25 16:45
查看更多