Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give the output of the first query(which has two values) as the input to the second?

i get 2 names as the output of the first query.... eg: paul,peter now this should be the input for the second query, which has to display paul's and peter's email ids....

like image 959
abhijithln Avatar asked Jan 20 '11 06:01

abhijithln


People also ask

How do you use the output of one query in another?

Use the results of a query as a field in another query. You can use a subquery as a field alias. Use a subquery as a field alias when you want to use the subquery results as a field in your main query. Note: A subquery that you use as a field alias cannot return more than one field.

How can I put two table data in one query?

To retrieve information from more than one table, you need to join those tables together. This can be done using JOIN methods, or you can use a second SELECT statement inside your main SELECT query—a subquery.


2 Answers

For nested queries I would strongly recommend WITH clause. It makes long complex queries order of magnitude easier to understand / construct / modify:

WITH 
   w_users AS( -- you can name it whatever you want
      SELECT id
        FROM users
       WHERE < long condition here >
   ),
   w_other_subquery AS(
      ...
   )
SELECT email_id
  FROM ...
 WHERE user_id IN (SELECT id FROM w_users)  
like image 188
Alexander Malakhov Avatar answered Sep 24 '22 10:09

Alexander Malakhov


You can use like this

LIKE

SELECT USER_ID,EMAIL_ID FROM USERS where user_id IN 
(SELECT PRODUCT_MEMBERS FROM PRODUCT WHERE PRODUCT_NAME='ICP/RAA');

Just use the IN clause '=' is used for matching one result

like image 30
Harish Avatar answered Sep 24 '22 10:09

Harish