Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Posts
    1

    Wink Unanswered: Count from two tables with the same column? how?

    Hi, i´d like to know how can i do COUNT of rows from two tables, which have the same column...
    For better understand logically it would be looking like this:

    SELECT count(*) FROM table1, table2 WHERE samecolumn = 1

    but this doesn´t work ...
    Hope u understand me and will help

  2. #2
    Join Date
    Sep 2003
    Posts
    14

    Re: Count from two tables with the same column? how?

    Hey,
    This query will give rise to "Ambigous Column name" error.
    Alter you query as follows and try
    SELECT count(*) FROM table1, table2 WHERE table1.samecolumn = 1
    and table2.samecolumn = 1


    Originally posted by stings
    Hi, i´d like to know how can i do COUNT of rows from two tables, which have the same column...
    For better understand logically it would be looking like this:

    SELECT count(*) FROM table1, table2 WHERE samecolumn = 1

    but this doesn´t work ...
    Hope u understand me and will help

  3. #3
    Join Date
    Jul 2003
    Posts
    35
    Hi,

    You could try...

    SELECT 'Table 1 Count' AS 'table', COUNT(table1.id) AS 'count' FROM tableName1 AS table1
    UNION ALL
    SELECT 'Table 2 Count' AS 'table', COUNT(table2.id) AS 'count' FROM tableName2 AS table2;

    That will return something like...

    +---------------+-------+
    | table | count |
    +---------------+-------+
    | Table 1 Count | 4 |
    | Table 2 Count | 1 |
    +---------------+-------+
    2 rows in set (0.00 sec)

    Chrs,
    Ash

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •