从多个表mysql php获取数据

时间:2017-11-18 08:21:43

标签: php mysql

我有一个db表,其中包含每个项目的每个用户的分数信息。我想得到用户未看到的项目的分数(未添加到购物车或愿望清单中)。 我有这个问题:

$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");

但是我遇到了以下致命错误:

  

未捕获的例外'例外'消息'错误:您有错误   在你的SQL语法中;查看与MySQL对应的手册   服务器版本,用于在UNION附近使用正确的语法(选择   来自customer_wishlist的product_id,其中customer_id =' 8')))

任何帮助?

1 个答案:

答案 0 :(得分:1)

如果客户ID是整数,请不要在其周围使用引号,并且不要使用unuseful()围绕选择UNION选择

  $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");