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

时间:2018-07-14 23:08:44

标签: php mysql laravel

我有一个数据库查询

       $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

1 个答案:

答案 0 :(得分:-1)

语法错误。

尝试一下...

$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.')'
);