我有一个数据库表,其中包含每个用户的每个项目的分数信息。我想获取用户未见过的商品(未添加到购物车或愿望 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/