0

我需要对 MySQL 表上的同一字段进行查询,但参数不同。这是我的查询。出现的问题是 MySQL 说 users_name 不明确。

SELECT projects_id, 
projects_users_id,projects_companies_id,projects_name,
projects_description,project_registered_date,
projects_users_id_register,
users.users_name as userInCharge,companies.companies_name as company,
users.users_name as user_register FROM projects 
LEFT JOIN users as userInCharge ON (users_id = projects_users_id) 
LEFT JOIN users as register ON (users_id=projects_users_id_register)
LEFT JOIN companies ON (companies_id = projects_companies_id) ORDER BY projects_id DESC 

我的目标是获取项目负责人和注册项目的人的ID。我该怎么做呢?

4

3 回答 3

2

由于以下LEFT JOIN条款,您会产生歧义:

LEFT JOIN users as userInCharge ON (users_id = projects_users_id) 
LEFT JOIN users as register ON (users_id=projects_users_id_register)

您需要指定users_id来自哪里(是的 - 我知道它们来自同一个表,但 SQL 有时有点慢)尝试:

LEFT JOIN users as userInCharge ON (userInCharge.users_id = projects_users_id) 
LEFT JOIN users as register ON (register.users_id=projects_users_id_register)
于 2013-10-24T22:54:39.620 回答
0

使用表别名而不是 users.users_name。

并且还在连接中。

于 2013-10-24T22:53:43.830 回答
0

您与users表一起加入userInChargeregister因此在您指定列时使用这些名称

userInCharge.users_name as userInCharge
register.users_name as user_register

代替

users.users_name as userInCharge
users.users_name as user_register
于 2013-10-24T22:54:03.580 回答