我需要在MySQL表上的相同字段上进行查询,但使用不同的参数。这是我的查询。出现的问题是MySQL说用户名是不明确的。
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。我该怎么做?
答案 0 :(得分: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)
答案 1 :(得分:0)
使用表别名而不是users.users_name。
还有加入。
答案 2 :(得分:0)
您与users
表格userInCharge
和register
一起加入,因此在指定列时使用这些名称
userInCharge.users_name as userInCharge
register.users_name as user_register
而不是
users.users_name as userInCharge
users.users_name as user_register