Wednesday, 15 May 2013

php - collect data from multiple tables with count and sum -


Select the amount (amount) in the form of

  amount, from the total table 1 as COUNT (userid) WHERE userid = '1111' Select the amount as the sum of all the sum (zodiac), the total number as COUNT (userid) to the table 2, where the user ID = '1111' union is selected as the sum of the amount (amount) , Total as COUNT (userid) from table 3 where UserID = '1111' Select all the total amount in the form of amount (amount), COUNT (userid) as total table 4 where userid = '1111' Association selected as the sum of all the sum (amount), COUNT as the userid, from the table 5 where the user ID = '1111'  <

proposed Solution:

The second option is to get data from all the tables separately and then display it.

Question:

I want to get data from multiple tables with a single query. The amount will be added and the user-aid will be counted from all the tables.

Additional Notes: This information must be gathered about user ID = 1111 and save in two varibables.

Userid = 1111, may not be present in some tables.

Expectations can be made:

Your total: $ total | And your zodiac: $ amount

Based on the suggestions, I have even received it so far and how am I trying to display it. It displays anything without any error: If I use a single query, the results are displayed OK Select Amount (Amount) as Amount (sms) as COALESCE ( Total Table 1 as the amount (amount), 0)

If you need a line for the column to sum all the tables The amount is then just select the external code on UNION . Now as you mentioned in the comment, some tables may not contain UserID You need to use COALESCE to make sure, That if the value of the null as the selected value is 0 the amount (amount (zodiac), 0) as the total amount (zodiac (data amount, 0)) Select the amount in (COALESCE (data amount, 0)), (total amount (zodiac), 0) total amount as a total amount, COALESCE (COUNT (userid)) 1 Where user ID = '1111' UNIUS all SELECT COALESCE (amount (zodiac), 0) in the form of amount, COALESCE (COUNT (userid)) as a total from WHERE userid = 1111 'union Select all COALESCE (amount (zodiac), 0) in the form of amount, COALESCE (COUNT (userid)) as table 3 where user ID = '1111' union all SELECT COALESCE (amount (zodiac), 0) COALESCE as the total number (COUNT (userid)) from table 4 where user ID = '1111' UNIUS Select all COALESCE (amount (zodiac), 0) in the form of amount, COALESCE (COUNTID) ( WHERE userid = '1111') from the table 5 of the data

and if you want to use joining then you should use the LEFT JOIN There is a need to separate and SUM separately for each table and then add it to the main total.


No comments:

Post a Comment