我的数据库查询哪里出错了?

qhhrdooz  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(241)

我有一个数据库查询

$extras = DB::select(

        'SELECT p.*
        FROM products p 
        WHERE required_product IS NULL 
        UNION ALL
SELECT p.* 
FROM products p, orders o 
WHERE p.required_product = o.product_id 
AND o.user_id =' .  $user->id .  'UNION ALL
SELECT p.* , "onlyOnce" product_type
FROM products p, orders o, only_once on 
WHERE p.required_product = o.product_id
AND p.id = on.product_id 
AND p.id NOT IN (SELECT product_id FROM orders WHERE o.product_id = p.id and o.user_id ='.$user->id.')'
);

以下是我的表格结构:
产品表:http://prntscr.com/k6jdv7
订单表: product_id,user_id 用户表: id,name,last_server,timestamps

0tdrvxhp

0tdrvxhp1#

语法错误。
试试这个。。。

$extras = DB::select(

    'SELECT p.*
    FROM products p 
    WHERE required_product IS NULL 
    UNION ALL
SELECT p.* 
FROM products p, orders o 
WHERE p.required_product = o.product_id 
AND o.user_id =' .  $user->id .  ' UNION ALL
SELECT p.* , "onlyOnce" product_type
FROM products p, orders o, only_once 
WHERE p.required_product = o.product_id
AND p.id = on.product_id 
AND p.id NOT IN (SELECT product_id FROM orders WHERE o.product_id = p.id and o.user_id ='.$user->id.')'
);

相关问题