我有一个数据库查询
$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.')'
);
这是我的表格结构:
“订单”表:product_id,user_id
用户表:id,name,last_server,timestamps
答案 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.')'
);