Error in inner query , where as inner query independently when run dont generate error

43 views Asked by At

Following is my query, this query is giving me following error Unknown column 'package_id' in 'where clause

insert into company_packages(
     package_product_id
    ,product_id
    ,company_id
    ,user_id
    ,expiry_date
    ,discount) 
values(
     (select id from package_products 
     where package_id=1 and product_id=5 and status=1 limit 1)
    ,5
    ,111
    ,116
    ,'2015-06-10'
    ,0)

But when i run this inner query i dont get any error

select id from package_products 
where package_id=1 and product_id=5 and status=1 limit 1
2

There are 2 answers

0
Jaylen On

This this query instead

INSERT INTO company_packages(package_product_id,product_id,company_id,user_id,expiry_date,discount) 
SELECT id, 5, 111, 116, '2015-06-10', 0 
FROM package_products 
WHERE package_id=1 AND product_id=5 AND status=1 
LIMIT 1
0
davek On

Try this so you are not mixing INSERT...VALUES and INSERT...SELECT syntax:

 insert into company_packages
 (
     package_product_id
   , product_id
   , company_id
   , user_id
   , expiry_date
   , discount
 ) 

select 
     id 
   , product_id
   , 111
   , 116
   , '2015-06-10'
   , 0
from package_products 
where package_id=1 
and   product_id=5 
and   status=1 
limit 1