Can anyone tell me why the following won't work? It complains of a syntax error near the join key word between the two selects.
SELECT * FROM ( select * from orders_products inner JOIN orders ON orders_products.orders_id = orders.orders_id where products_id = 181) as A join SELECT * FROM ( select * from orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id where products_id = 180) as B on A.orders_id=B.orders_id
Basically my first SELECT
pulls all the order info for a certain product from one table and pulls the quantity ordered from another and joins them together. The second SELECT
does the same thing for another product.
Now, I have
_______A_________ _______B_________
O_ID P_ID Q O_ID P_ID Q
1 180 3 1 181 11
2 180 9 2 181 6
3 180 5 3 181 3
And, using another join I want to get
Q_ID P_ID1 Q1 P_ID2 Q2
1 180 3 181 11
2 180 9 181 6
3 180 5 181 3
Maybe I am taking a wrong approach here. Any suggestions?
UPDATE: Here is what worked for me after pointers by RedFilter:
(SELECT * FROM ( SELECT * FROM orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id WHERE products_id =181) AS A LEFT JOIN ( SELECT * FROM orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id WHERE products_id =180) AS B ON A.orders_id = B.orders_id ) UNION ( SELECT * FROM ( SELECT * FROM orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id WHERE products_id =181 ) AS C RIGHT JOIN ( SELECT * FROM orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id WHERE products_id =180 ) AS D ON C.orders_id = D.orders_id )
To combine two or more SELECT statements to form a single result table, use the set operators: UNION, EXCEPT or INTERSECT.
You have four options for combining two complete sentences: comma and a conjunction ("and," "but," "or," "for," or "yet") semicolon and a transitional adverb, like "therefore," "moreover," or "thus"
MySQL UNION operator To combine result set of two or more queries using the UNION operator, these are the basic rules that you must follow: First, the number and the orders of columns that appear in all SELECT statements must be the same. Second, the data types of columns must be the same or compatible.
Not sure what you are trying to do, but you have two select clauses. Do this instead:
SELECT * FROM ( SELECT * FROM orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id WHERE products_id = 181) AS A JOIN ( SELECT * FROM orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id WHERE products_id = 180) AS B ON A.orders_id=B.orders_id
Update:
You could probably reduce it to something like this:
SELECT o.orders_id, op1.products_id, op1.quantity, op2.products_id, op2.quantity FROM orders o INNER JOIN orders_products op1 on o.orders_id = op1.orders_id INNER JOIN orders_products op2 on o.orders_id = op2.orders_id WHERE op1.products_id = 180 AND op2.products_id = 181
You should use UNION if you want to combine different resultsets. Try the following:
(SELECT * FROM ( SELECT * FROM orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id WHERE products_id = 181) AS A) UNION (SELECT * FROM ( SELECT * FROM orders_products INNER JOIN orders ON orders_products.orders_id = orders.orders_id WHERE products_id = 180) AS B ON A.orders_id=B.orders_id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With