php - mysql php从多个表中获取数据

标签 php mysql

我有一个数据库表,其中包含每个用户的每个项目的分数信息。我想获取用户未见过的商品(未添加到购物车或愿望 list 中)的分数。 我有这样的疑问:

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

但是我遇到了以下 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') ))

有什么帮助吗?

最佳答案

如果客户 ID 是整数,请勿在其周围使用引号,也不要在 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");

关于php - mysql php从多个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47364043/

相关文章:

php - 如何使用 PDO 通过 select 命令调用过程?

php - 我如何根据 Wordpress 中缩略图的存在进行排序?

php - 动态构建表单

php - 正则表达式捕获两位数字仅当字符串中没有其他数字时

php - 三维(或更多)标签系统? PHP/MySQL 数据库架构

php - 如何用PHP检测网速?

mysql - 用于更复杂表结构的 Eloquent/Query Builder

mysql - 删除行并比较月份

MySQL 在单个请求中同一个表中存在多个 COUNT

MySQL trim() 不删除字符串中的空格