简体   繁体   中英

MYSQL Left join fails with "unknown column" in java

I have a Spring Boot application running Spring boot 2.2.5, mysql-connector-java version 5.1.38

In my application I have the following query:

SELECT
  role.id AS roleid,
  roles.id,
  role_name,
  role_description,
  contact_name,
  roles.employee_mail,
  contact_phone
FROM
  rcsa.fs_role AS ROLE
  LEFT JOIN rcsa.fs_customer_roles_external AS roles
    ON roles.fs_role_id = role.id AND fs_customer_id = :account
  LEFT JOIN rcsa.fs_business_service_contacts AS cont
    ON roles.employee_mail = cont.contact_email
WHERE role_type_id = 2
GROUP BY role.id,
  employee_mail

When I run this query in my mysql admin console, it returns the following result: 正确结果

Which is what I want it to return.

However, when I try to run this query from inside my Spring boot appliation, it throws an exception:

PreparedStatementCallback; uncategorized SQLException for SQL [select role.id as roleid, roles.id, role_name, role_description, contact_name, roles.employee_mail , contact_phone from rcsa.fs_role as role left join rcsa.fs_customer_roles_external as roles on roles.fs_role_id=role.id and fs_customer_id=? left join rcsa.fs_business_service_contacts as contacts on contacts.contact_email=roles.employee_mail where role_type_id=2 group by role.id, employee_mail]; SQL state [S0022]; error code [0]; Column 'contact_email' not found.; nested exception is java.sql.SQLException: Column 'contact_email' not found

I have tried swapping the order og the columns in the second on clause, but to no avail. I guess the error comes from the order of my joins, where I start with the fs_role table, but this is intentionally so that any unassigned roles also get returned.

EDIT:

In my original question the queries were not identical, as I had been experimenting to see if I could find a solution, the problem still persists when the queries are identical.

EDIT 2:

I turned on the mysql transaction log and both the queries are present in it:

Tcp port: 3306  Unix socket: /tmp/mysql.sock
Time                            Id  Command Argument
2020-03-06T12:39:15.942813Z     8   Query   SHOW GLOBAL STATUS
2020-03-06T12:39:18.568670Z     9   Query   SET autocommit=0
2020-03-06T12:39:18.569841Z     9   Query   select role.id as roleid, roles.id, role_name, role_description, contact_name, roles.employee_mail , contact_phone from rcsa.fs_role as role  
                                            left join rcsa.fs_customer_roles_external  as roles    on roles.fs_role_id=role.id and fs_customer_id=1010499  
                                            left join rcsa.fs_business_service_contacts as cont on roles.employee_mail=cont.contact_email  
                                         where role_type_id=2 group by role.id, employee_mail
2020-03-06T12:39:18.574042Z     9   Query   rollback
2020-03-06T12:39:18.574299Z     9   Query   SET autocommit=1
2020-03-06T12:39:18.952383Z     8   Query   SHOW GLOBAL STATUS
2020-03-06T12:39:20.048826Z     2   Query   select role.id as roleid, roles.id, role_name, role_description, contact_name, roles.employee_mail , contact_phone from rcsa.fs_role as role 
                                            left join rcsa.fs_customer_roles_external  as roles    on roles.fs_role_id=role.id and fs_customer_id=1010499 
                                            left join rcsa.fs_business_service_contacts as cont on roles.employee_mail=cont.contact_email 
                                            where role_type_id=2 group by role.id, employee_mail

The first query originates from my java code and the second from my mysql workbench.

These are two different queries

select r.id role_id
     , e.id
     , role_name
     , role_description
     , contact_name
     , e.employee_mail 
     , contact_phone 
  from rcsa.fs_role r 
  left 
  join rcsa.fs_customer_roles_external e
    on e.fs_role_id = r.id 
  left 
  join rcsa.fs_business_service_contacts c
  on e.employee_mail = c.contact_email  
 where role_type_id = 2 

   and fs_customer_id = :account 

 group 
    by r.id
     , employee_mail

select r.id role_id
     , e.id
     , role_name
     , role_description
     , contact_name
     , e.employee_mail
     , contact_phone 
  from rcsa.fs_role r 
  left 
  join rcsa.fs_customer_roles_external e
    on e.fs_role_id = r.id 

   and fs_customer_id =? 

  left 
  join rcsa.fs_business_service_contacts c
    on c.contact_email = e.employee_mail  
 where role_type_id = 2 

 group 
    by role.id
     , employee_mail

It turns out that the error was not from the MYSQL server, but from the java application. I was fetching contact_email from the resultset and populating an object with this, and this was where the error occured.

尝试对所有列都这样使用select role.id as roleid => select role.id as 'roleid'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM