Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Stank0V01's avatar

So sql must be that

@Tray2


SELECT p.* from Products p where required_product_id IS NULL 

UNION ALL

SELECT p.* FROM products p orders o WHERE p.required_product_id = o.product_d
AND o.user_id = 1 

UNION ALL

SELECT p.* FROM products p where p.max_buys = 1 

Stank0V01's avatar
SELECT p.* FROM products p WHERE required_product_id IS NULL 
UNION ALL
SELECT p.* FROM products p, orders o WHERE p.required_product_id = o.product_id 
AND o.user_id = 1
UNION ALL
SELECT p.* FROM products p, orders o 
WHERE p.required_product_id = o.product_id
AND p.max_buys > 0

I make that query but it return that

http://prntscr.com/k6o52y

Tray2's avatar

The last select should be something like


SELECT p.* FROM products WHERE p.id NOT IN (SELECT product_id FROM orders WHERE product_id = p.id AND o.user_id = 1) AND p.max_buys = 1

It will not show any products already bought.

You probably need to disciminate further som you don't get duplicate rows.

Stank0V01's avatar

:D Now the most hard thing is how to make not row duplicate

Stank0V01's avatar

It not work with DISTINCT

SELECT p.* FROM products p WHERE required_product_id IS NULL 
UNION 
SELECT  p.* FROM products p, orders o WHERE p.required_product_id = o.product_id 
AND o.user_id = 1
UNION DISTINCT
SELECT p.`*` FROM products p, orders o WHERE p.id NOT IN (SELECT product_id FROM orders WHERE product_id = p.id AND o.user_id = 1)
AND p.max_buys = 1;

http://prntscr.com/k6od0q @Tray2

Tray2's avatar

Run the sql one by one and add more filters as you go until you get only the records you want for each query then set them back together again.

Stank0V01's avatar

I fuck'up my brain :D with that query :d i thinking to not make it with upgrades @Tray2 Its way easy to use eloquent so for that i use laravel

Previous

Please or to participate in this conversation.