1

The result i want,'

price1   |  price2 
1. 10000 |  2. -100 
3. 20000 |  4. -200

the queries that will be selected,

  1. select total_price from cloud_payment where user_id='snoopy99' and demandnum= '201307-charge0000040C'

  2. select unpaid_price from gcloud.cloud_service_basic where user_id='snoopy99' and month=7

  3. select total_price from gcloud.cloud_payment where user_id='snoopy99' and demandnum= '201308-charge0000040C'

  4. select unpaid_price from gcloud.cloud_service_basic where user_id='snoopy99' and month=8

I want 1,3 to be in the first column 2,4 to be in the second column.

Any good idea?

2 Answers 2

3

Try.

select total_price as price from cloud_payment where user_id='snoopy99' and (demandnum= '201307-charge0000040C' OR demandnum= '201308-charge0000040C')
UNION ALL
select unpaid_price as price from gcloud.cloud_service_basic where user_id='snoopy99' and ( month=7 OR month=8)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use JOIN to select a merged record from your service and payment tables, then UNION to select two such records. Something like this:

SELECT total_price, unpaid_price
    FROM  cloud_payment cp
    JOIN  gcloud.cloud_service_basic cs ON cp.user_id = cs.user_id
    WHERE cp.user_id = 'snoopy99'
    AND   cp.demandnum = '201307-charge0000040C'
    AND   cs.month = 7
UNION
SELECT total_price, unpaid_price
    FROM  cloud_payment cp2
    JOIN  gcloud.cloud_service_basic cs2 ON cp2.user_id = cs2.user_id
    WHERE cp2.user_id = 'snoopy99'
    AND   cp2.demandnum = '201308-charge0000040C'
    AND   cs2.month = 8
;

Example SQLFiddle.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.