0

I have a query:

select
    distinct orders.id,
    orders.amount,
    CONCAT(users.firstName, ' ', users.lastName) as username,
    users.email,
    users.id as userId,
    users.country,
    users.phoneNumber,
    GROUP_CONCAT(DISTINCT products.name SEPARATOR ' ') as productsList,
    orders.invoiceUrl,
    coupons.name as couponName,
    users.firstName,
    users.lastName
  from orders
  inner join users
    on orders.userId=users.id
  left join orderItems
    on orderItems.orderId=orders.id
  left join products
    on orderItems.productId=products.id
  left join coupons
    on orders.coupon=coupons.id
  
  where 
        orders.id = 'mike'
        OR CONCAT(users.firstName, ' ', users.lastName) like '%mike%'
        OR users.email like '%mike%'
      
  group by orders.id
  order by orders.created desc
  limit 0,25;

It works in shell:

enter image description here

But, it doesn't work with mysql2. It throws an error:

Unknown column 'users.firstName' in 'where clause'

enter image description here

What is causing this error with node-mysql2?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
mr0b0t0
  • 53
  • 8
  • Are the two definitely using the same database? It's easy to have one pointing to a dev db and the other to a different version of that db. – Andrew Morton Jul 27 '22 at 17:44
  • Yes, you have my word – mr0b0t0 Jul 27 '22 at 17:45
  • Looking at your second screenshot, it seems you are not executing the same query in your node-mysql2 application. For example, the query does not join to the `users` table. There is no join at all; you have `...from orders where...` which means references to any column of the `users` table will not work. – Bill Karwin Jul 27 '22 at 17:50
  • 1
    Query you are showing in the error log and the query you've got yourself printed in the question are both different. Please make sure to use the same query or if possible try adding the DB name with table name. Tips:- print query first and compare with the orignal query to get whether you're skipping something or not – Nouman Ahmad Jul 27 '22 at 17:54
  • @mr0b0t0 If you want to make sure that the same SQL is used, you could use a stored procedure for it. There are many short tutorials on how to create them, e.g. [Working with MySQL Stored Procedures](https://www.red-gate.com/simple-talk/databases/mysql/working-with-mysql-stored-procedures/); [Learn MySQL: The Basics of MySQL Stored Procedures](https://www.sqlshack.com/learn-mysql-the-basics-of-mysql-stored-procedures/). – Andrew Morton Jul 27 '22 at 17:59

0 Answers0