简体   繁体   中英

get data from multiple tables mysql php

I have a db table that contains a score information for each user with each item. I want to get the scores of the items not seen (not added to the shopping cart or wish list) by the user. I have this query:

$query = $this->db->query("
select product_id
     , score 
    from score where ( customer_id= '$customer_id' and product_id not in ( 
        ( select product_id from cart where customer_id= '$customer_id' ) 
        UNION 
        ( select product_id from customer_wishlist where customer_id= '$customer_id' ) 
    ) ) 
    order by score desc 
    limit 4");

but I've got the following fatal error:

Uncaught exception 'Exception' with message 'Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION (select product_id from customer_wishlist where customer_id= '8') ))

any help?

If the customer id is integer don'y use quotes around it and don't use unuseful () around select UNION select

  $query = $this->db->query("select 
       product_id  
      , score 
      from score 
      where ( customer_id= '$customer_id' and product_id not in ( 
          ( 
            select product_id from cart where customer_id= $customer_id  
            UNION 
            select product_id from customer_wishlist where customer_id= $customer_id
          ) 
      ) ) 
      order by score desc 
      limit 4");

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