Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

joining two select statements

Tags:

sql

join

mysql

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 )  
like image 519
Codrguy Avatar asked May 17 '11 19:05

Codrguy


People also ask

Can I join two SELECT statements?

To combine two or more SELECT statements to form a single result table, use the set operators: UNION, EXCEPT or INTERSECT.

How do you join two statements?

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"

Can MySQL join two SELECT statements?

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.


2 Answers

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 
like image 176
D'Arcy Rittich Avatar answered Sep 21 '22 19:09

D'Arcy Rittich


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) 
like image 34
alexn Avatar answered Sep 19 '22 19:09

alexn