2

Finding Total Records in Different Tables in a Single Query

 2 years ago
source link: https://www.codesd.com/item/finding-total-records-in-different-tables-in-a-single-query.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Finding Total Records in Different Tables in a Single Query

advertisements

Currently I m using this query ,Is there any substitution for this query,which will work more faster .

SELECT
    SUM(result1),
    SUM(result2),
    SUM(result3)
FROM (
    (
        SELECT
            0 as result1,0 as result2,COUNT(*) as result3
        FROM
            table1
    )
    UNION
    (
        SELECT
            count(*) as result1,0 as result2,0 as result3
        FROM
         table2
    )
    UNION
    (
        SELECT
            0 as result1,count(*) as result2,0 as result3
        FROM
            table3
    )
    ) as allresult


Alternate solution of above query is as below:

SELECT (SELECT COUNT(1) FROM table2) AS result1,
       (SELECT COUNT(1) FROM table3) AS result2,
       (SELECT COUNT(1) FROM table1) AS result3;

Related Articles

How to combine records from different tables and run the query using SQL?

The original question is unclear so I modify the question and ask again original question :How to combine records from different tables? There are two worksheets in same workbook that have the same structure-same field names. for example : Table 1 -

how to calculate the difference between 2 fields in 2 different tables using a single query?

Table Users : user_id , credits_available Table Survey : user_id, survey_id, credits_required Now I want to calculate the difference between credits_available and credits_required for a particular user_id and specified survey_id ? Is it possible to w

SQL counts records in multiple tables using a single query

I have a list of tables for example: mytableA mytableB mytableC The tables all have same column (timestamp). I can do a count on each table individually: select count(*) from mytableA where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000'; select

Database query to get the sum of the number of different tables in a single query

I am writing a query in Mysql database, in which Query 1 returns count() say result is 10 and Query 2 returns Count() say result is 30 But I want to get the result as 40, which is sum of both what are my options to get a single query giving me the re

Get the sum of the different column of the different table in a single query

I am getting wrong sum answer by this query - does anyone have any idea what I'm doing wrong? I am getting the first sum twice, the second sum 3 times more than the actual value and so on. SELECT ogr.Region_ID, ogr.Region_Name, Sum(daf.Total_Allowanc

update of two tables with a single query

Possible Duplicate: MySQL, update multiple tables with one query I'd like to know how to update two different tables with a single query, using MySql and C#. Here is my query to read the data from the database. string sql = @"SELECT clb.clube AS nome

Get the total records in multiple tables as a view table (mysql)

Is there a way to get the total records in each table then put it in a view table. My current query is CREATE VIEW view_summary AS SELECT COUNT(*) as total_records FROM users WHERE role_id = 2 UNION ALL (SELECT COUNT(*) FROM users WHERE role_id = 1)

How to use the ORMLite query builder to get the total records in a table

Similar to select count(*) from tablename; what should be query in ORMLITE i tried something like int total = dao.queryBuilder().("select count(*)"); How to use the ORMLite query builder to get the total records in a table ORMLite has a Dao.coun

Find duplicate records in the table by more than one criterion

+-------------------------+ | SKU | UPC | NAME | INVID +-------------------------+ | 001 | 111 | NAME1 | 1 | 001 | null | NAME2 | 2 | 0-1 | 111 | NAME3 | 3 | -01 | 111 | NAME4 | 4 | 002 | 222 | NAME5 | 5 | 002 | null | NAME6 | 6 | 003 | 333 | NAME7 |

How do I delete records from the child table and the parent table in a single query?

I have a master table and a details table. On delete cascade is not specified. I want delete child record as well as master record in a single query. Suppose I want to delete EmployeeDetails and Employee record where EmpID=20 using a single query. Is

inserting into different tables via a mysql query

how can i insert in 3 different tables through one mysql query that is i want to write one mysql query that will insert in 3 different tables.i have basically html form in which there are different tables involved so i will insert data collected from

want to display positive and negative values ​​in different columns via a single query

I have one column table with negative and positive values and want to display positive and negative values in different columns thru a single query. Column -10000 -17000 16000 25000 output should be like A B ----------------- -10000 16000 -17000 2500

How to further optimize this MySQL table for a single query

I have a InnoDB MySql Geo ID table that has ~ 1 million rows. The table structure is this: CREATE TABLE `geoid` ( `start_ip` int(11) NOT NULL, `end_ip` int(11) NOT NULL, `city` varchar(64) NOT NULL, `region` char(2) NOT NULL, PRIMARY KEY (`start_ip`,

Selection of a publication and all its labels from two tables with a single query

I have two tables; one with post data and in the other each row has a tag and post id (FK). Is it possible to select a post with all of its tags from these two tables with a single Query? How? ThanksI guess you could turn around the query SELECT * FR

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK