Counting rows in mysql

what command to i use to count the number of rows in a table and to do this will i use the statement
while (resultset.next()) {                                                                                                                                                                                                                                                               

You use the SQL statement "SELECT count(*) from WhateverTable", get a ResultSet from it, and then use this code to get the number:rs.next();
int rows = rs.getInt(1);

Similar Messages

  • Count rows from multiple tables using SQL only

    Hi, I know this has probably been answered before, but I couldn't find the answer anywhere. Please help.
    I'd like count(*) [rows] for all tables in database using SQL only - no PL/SQL
    The result should be something like:
    Table RowCount
    DBA_TABLES 1000
    DBA_USERS 50
    etc.
    Thanks!

    offcource write this script:
    create or replace procedure count_tables (ip_schema VARCHAR2)
    is
    lv_owner VARCHAR2(100);
    lv_table_name VARCHAR2(100);
    lv_sql_statement VARCHAR2(2000);
    lv_count_table NUMBER;
    CURSOR c1 IS
    SELECT owner, table_name
    FROM all_tables
    WHERE owner = ip_schema
    ORDER BY table_name;
    begin
    dbms_output.put_line ('+--------------------------------------------------------------------+');
    dbms_output.put_line ('¦ | | ¦');
    dbms_output.put_line ('¦ Schema Name | Table Name | Number of Rows ¦');
    dbms_output.put_line ('¦ | | ¦');
    dbms_output.put_line ('¦------------------------------------------------------------------¦');
    OPEN c1;
    LOOP
    FETCH c1 INTO lv_owner , lv_table_name;
    EXIT WHEN c1%NOTFOUND;
    lv_sql_statement := 'SELECT count(*) FROM ' || lv_owner || '.' || lv_table_name;
    EXECUTE IMMEDIATE lv_sql_statement INTO lv_count_table;
    IF lv_count_table > 0 THEN
    dbms_output.put_line ('| '||rpad(lv_owner, 14, ' ')||'| '|| rpad(lv_table_name, 32, ' ')||'| '|| rpad(lv_count_table, 16, ' ')||' |');
    -- dbms_output.put_line ('|---------------|---------------------------------|------------------|');
    END IF;
    END LOOP;
    CLOSE c1;
    dbms_output.put_line ('+--------------------------------------------------------------------+');
    exception
    WHEN OTHERS THEN
    dbms_output.put_line ('owner: '||lv_owner||' - table: '||lv_table_name||' - '||sqlerrm);
    end count_tables;
    set serveroutput on size 1000000
    exec count_tables
    drop procedure count_tables;

  • Count rows from several tables

    hello,
    im trying to count row from multiple tables
    for example i need the select statement to produce the following
    table_name count
    table1 5
    table2 6
    table3 3
    i came up with the following script but it counts the number of tables i have
    select object_name, (select count(*) from user_tables where table_name = object_name) from all_objects
    where object_type = 'TABLE'

    Manik wrote:
    May be possible:
    Check this:
    SELECT table_name,
    TO_NUMBER (
    EXTRACTVALUE (
    xmltype (
    DBMS_XMLGEN.getxml ('select count(*) c from ' || table_name)),
    '/ROWSET/ROW/C'))
    COUNT
    FROM (select * from all_tables where table_name in ('TABLE1','TABLE2'))
    WHERE owner = 'SCOTT';Cheers,
    Manik.Awesome Manik... Just too good. Thanks.
    I wish i could have given you the 'Correct' points. ;-)
    Can you please explain the logic in brief? Will be helpful for everybody to understand...

  • SQL Developer 2.1 EA - count rows with filter on a table

    This is a small problem that I came across today while using 2.1 EA. I opened a table and did a filter to narrow down the results of the table. Then when I right clicked on the search results and clicked on 'Count Rows'....I get the count of the rows in the table and not my search results.
    Is this a feature or a bug?
    Thanks,
    Mike

    In EA2, we get both the total and the filtered total, nice.
    Only remark is we don't get a progress indicator anymore, and thus can't cancel a query that might take up hours to finish!
    Please log a bug for this.
    Thanks,
    K.

  • Count rows in an internal table

    Anybody how knows how I can count rows in my internal table?

    Hi,
    Data: lines like sy-tabix.
    DESCRIBE TABLE ITAB LINES LINES.
    where itab is your internal table.
    This will work out.
    Please award sutiable points .
    Regards,
    Irfan

  • How to fetch last inserted row in MySQL

    Hi,
    I am trying to get the last inserted row in MySql..but not able to fetch it.
    this is what i used
    i have one column order_id which is auto_increment
    SELECT * FROM tablename WHERE order_id=(SELECT MAX(order_id) FROM tablename)
    any help is appreciated .
    the usage of lastinsert() method is also not successful
    chintan

    Hello
    Given you have a date column, you can use it to determine the latest row. However, there is still a chance that two rows were inserted at exactly the same time in which case you will need something in addition to your date column to decide which is the latest...
    The first option uses a sub query and the second uses analytics, as does the 3rd - however with the 3rd, if 2 rows have exactly the same date and time, it will pick one at random unless you include another column in the order by to determine the latest...
    select
    FROM
        your_table
    WHERE
        your_date_column = (SELECT MAX(your_date_column) from your_table)
    SELECT
    FROM
            select
                a.*,
                MAX(your_date_column) OVER() max_date_column
            FROM
                your_table a
    WHERE
        your_date_column = max_date_column
    SELECT
    FROM
            select
                a.*,
                ROW_NUMBER OVER(ORDER BY your_date_column DESC) rn
            FROM
                your_table a
    WHERE
        rn = 1HTH
    David

  • How to set Tile count column or count row?

    How to set Tile count column or count row? If not can do, How
    to adjust count row or count column?
    Thx for all idea.

    The Tile container's number of columns and rows is calculated
    based on each child's width and height (or based on tileWidth and
    tileHeight which you can set). If you need more control over the
    layout, use Grid.

  • Can i count row from ResultSet ?

    I would like to count row from ResultSet for
    take it into my array object because i must know number of row before create array object.
    Example:
    ResultSet rset = stmt.executeQuery("select * from user ");
    /*i = amount of ResultSet*/
    User[] user = new User;
    int l=0;
    while (rset.next()){
    user[l] = new User();
    user.name = rset.getString(1);
    l++;

    Hi,
    As per my knowledge there is no method by which you can get the count of items in a resultset directly. You will have to loop through the reseltset and set a variable for count. In your specific case I would advise you to use a Vetor instead of an array so that you need not bother about the size.
    ResultSet rset = stmt.executeQuery("select * from user ");
    Vector user = new Vector();
    while (rset.next()){
    user.addElement(rset.getString(1));
    Now you will have a Vector that holds the user info. To retrieve the user info loop through the Vector.
    for (int i; i<user.size(); i++){
    userName = user.elementAt(i);
    Hope I was of some help.
    cheers!!!
    Nish

  • Count rows that are max timestamp

    Hi,
    I'm trying to run a query that will count only rows that are max timestamp.
    i have a submit table which has dates of submissions, however some application numbers have multiple rows in this table. I need to count only one row per application and it has to be the latest submission data.
    I have tried a subquery and its not working, I'm stuck at this point.
    Thanks for help :)
    example table
    pk app # submit_date
    12 test-1 02222011 13:30
    13 test-2 02232011 09:45
    14 test-1 02232011 09:51
    how do i count rows but based on max timestamp?

    select
    count(s.pk)
    from
    submit s
    where exists (select s1.app#, max(s1.submit_date) from submit s1 group by s1.app#)
    I dont really understand what you are doing there.
    SQL> WITH T
      2       AS (SELECT 12 pk, 'TEST-1' app#, SYSDATE submit_date FROM DUAL
      3           UNION ALL
      4           SELECT 13 pk, 'TEST-2' app#, SYSDATE + 1 submit_date FROM DUAL
      5           UNION ALL
      6           SELECT 14 pk, 'TEST-1' app#, SYSDATE + 2 submit_date FROM DUAL)
      7  SELECT * FROM T;
            PK APP#   SUBMIT_DA
            12 TEST-1 23-FEB-11
            13 TEST-2 24-FEB-11
            14 TEST-1 25-FEB-11
    SQL> WITH T
      2       AS (SELECT 12 pk, 'TEST-1' app#, SYSDATE submit_date FROM DUAL
      3           UNION ALL
      4           SELECT 13 pk, 'TEST-2' app#, SYSDATE + 1 submit_date FROM DUAL
      5           UNION ALL
      6           SELECT 14 pk, 'TEST-1' app#, SYSDATE + 2 submit_date FROM DUAL)
      7  SELECT   pk, app#
      8    FROM   (SELECT pk, app#, ROW_NUMBER () OVER (PARTITION BY app# ORDER BY submit_date DESC) rn
    FROM T)
      9   WHERE   rn = 1;
            PK APP#
            14 TEST-1
            13 TEST-2
    SQL>

  • Serialization Objects in column of row in MySQL

    Hi :)
    I have class with different methods, each of this methods make large operation in database (Hibernate3 + MySQL).
    I consider situation when my application may crush, and in proccess of recovering I recover objects-parameters for my different methods.
    I want to use simple Java serialization object (ObjectInputStream and ObjectOutputStream) and save this bytes in column of row in MySQL, but
    in future I'm afraid that fields of parameter-objects of my methods changed (will be add new fields, deleted fields), and I can't deserialize my parameter-objects from MySQL. I know one decision - serialization objects in XML and save XML in DB, but may be somebody offers other variants of saving my object-parameters more quickly than XML.
    Thanks all :) New member of Sun Java Forum, Alexander Stepanov.

    Hi :)
    I have class with different methods, each of this methods make large operation in database (Hibernate3 + MySQL).
    I consider situation when my application may crush, and in proccess of recovering I recover objects-parameters for my different methods.
    I want to use simple Java serialization object (ObjectInputStream and ObjectOutputStream) and save this bytes in column of row in MySQL, but
    in future I'm afraid that fields of parameter-objects of my methods changed (will be add new fields, deleted fields), and I can't deserialize my parameter-objects from MySQL. I know one decision - serialization objects in XML and save XML in DB, but may be somebody offers other variants of saving my object-parameters more quickly than XML.
    Thanks all :) New member of Sun Java Forum, Alexander Stepanov.

  • What is the problem with native dynamic sql when counting rows in all table

    what is the problem with native dynamic sql when counting rows in all table?Giving an error "table or view does not exist". Thanks.
    DECLARE
    v_sql_string varchar2(1000);
    v_no_of_rows number;
    BEGIN
    for i in ( select table_name from all_tables )
    loop
    v_sql_string := ' select count(1) from ' || i.table_name;
    dbms_output.put_line( v_sql_string );
    --execute immediate v_sql_string into v_no_of_rows;
    end loop;
    END;

    Usually your problem can be described with 'Who cares'. I mean, for what reason do you do this? I doubt that there's a business need to get 100 % accurate answers for this. Normally such things are used to get a picture about the growth of data.
    Personally I would prefer to have up-to-date statistics for all tables and just query the number of rows from there. Sufficient for me in < 99 % of all cases.
    Just my $ .02...

  • Counting rows in a mysql query

    Hey
    How do i count the rows in a mysql query?
    I have tried to make a while loop (as you can see in the code), but if i make this while loop i cant use the same query to get the database data.
    Doesnt java have a function that counts the rows?
    public DefaultTableModel searchCategory(String searchString){
            String returnString = "";
            try{
                RS = connection.executeQuery("SELECT name, price, products_type_id FROM katrinelund_products WHERE products_type_id = (SELECT products_type_id FROM katrinelund_products_type WHERE name LIKE '%"+searchString+"%')");
                int rowCount = 0;
                while(RS.next()){
                    rowCount++;
                String[] columnNames = {"Navn", "Pris", "Produkt type"};
                Object[][] data = new Object[rowCount][columnNames.length];
                int i=0;
                while(RS.next()){
                    data[0] = RS.getString(1);
    data[i][1] = RS.getString(2);
    data[i][2] = RS.getString(3);
    i++;
    table = new DefaultTableModel(data, columnNames);
    catch(SQLException E){
    System.out.println("Der skete en fejl" + E.getMessage());
    return table;
    }NOTE: the connection to the database is stored in the connection variable.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    yawmark wrote:
    The SELECT COUNT(*) cannot be fetched among other data, so in this case you must send two queries. Then there is much simplier to use the method I mentioned above.While it may be "simpler", it's not supported in a non-trivial number of drivers. So there are a number of situations in which your "simple" solution will not work. I assume you've been fortunate enough to work only with drivers that do support the functionality you recommend, but I can also only assume that you haven't had much experience beyond that.I apologize for my spelling errors. I'm not a native englishman.
    yawmark wrote:
    Knowing the amount of rows a resultset returns can be interesting if you're making some kind of search engine. Knowing the amount of data returned for a particular query can certainly be useful, and we've presented some reasonable means of obtaining that kind of information that don't rely on potentially unsupported features.
    I don't now about unsupported features. The ResultSet is (as far as I know) not tied to a specific SQL database. This, and all of it's methods is in the java.sql.ResultSet interface. Then all classes implementing this must have these methods. Am I not right?
    yawmark wrote:
    Stop dealing with however it's necessary or not.Sorry, partner, but you don't get to decide what I post here.
    This guy wants this problem to be solved, so apparentley he has a usage for it...And what you're apparently failing notice is that there are other solutions (better, in many cases) than the solution which you posted. Don't make the mistake of thinking your suggestion is golden. It isn't. Besides, there are no shortage of beginners that want the "problem" of i = i++ to be "solved"; no doubt they think they have an apparent usage for it, too. ~I'm well aware of that you decide on your own what to post. I just don't see how it is necessary to discuss the matter of necessity in this case. The problem still remains.
    I don't want any fuss here. I'm just so sick of everyone who yells "you don't need to know that". I haven't decried any of the other solutions (at least it wasn't my point), all I said was that it's not a good idea to send multiple queries to the database when you already have the answer three rows of code away. In some rare situations, the data may have been changed during the short period of time as the database is finished with the first query before you send the next one.

  • Counting Rows in a Table

    I'm new at this mysql stuff and can't seem to figure out how to write the code for querying the database for the number of rows in a table. I can get a connection to the database and have done some basic commands like SELECT * FROM TABLE using rs.next().
    Can somebody help me out with the java bit for this..?
    select (count*) from myTable;
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM myTable);
    Thanks,
    amy

    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM myTable");
    while( rs.next() )
    // Should run 1 time only
    System.out.println( "count of rows: " + rs.getInt( 1 ) );
    rs.close();Try out, if I haven't overseen something.
    The trick is: your query brings you a resultset containing 1 row with 1 column: the int value.

  • Excel ActiveX: Count Rows

    Hi, I am trying to simply count the used rows in an excel spreadsheet (which I have done in the past).
    Attached is a simple vi which I am trying to get to work.  On my development PC, this works fine, counts the rows etc...on this PC I have Excel 2003.
    When I create an EXE out of this VI and put it on another PC with Excel 2000, it returns "0" for the number of used rows.
    Can ANYONE help me here, this is driving me insane.
    Working in 7.1 (can use 7.0 or 8.0 if it makes it easier)
    Thanks,
    Ryan
    Ryan
    LV 7.1
    Attachments:
    test.vi ‏59 KB

    It is limiting but I place the biggest blame on Microsoft for changing the properties and methods so much between different releases. You could put all of your Excel functions in a library that you call dynamically. That way, you don't have to re-build the exe but just distribute the library specific to whatever version of Excel you need to support. Unless you really need some specific Excel formatting, you could also save to comma/tab delimited text files. All versions of Excel can open them and you won't have to make a career out of modifying your application for as long as Excel is going to be around.

  • Counting rows respect to the last status assumed

    Hi,
    I'm analyzing dossier data in a PowerPivot table.
    In particular, I've three tables: the Dossier table, the Calendar table (date, week, month, quarter, semester and year) and the Status table for dossier.
    In a same day, a dossier can change his status more times (from created to active, from active to closed, and so on): so in the Dossier table I've more dossier occurences with a status date and a sequence column to replace the time. For a new date, the sequence
    column restarts to 1.
    I need to count the dossier respect to the selected period (a specific date, a specific month, a specific year, and so on) by considering only the dossier with the last status date minor or equal than the selected period.
    Fe, for these dossier data:
    Dossier id      Status      Status_Date     Sequence
          1            Created    11/02/2015           1
          1            Active       11/02/2015          2
          1            Closed      11/02/2015          3
    I must consider the closed dossier row with sequence = 3 when I select '11/02/2015' date from my calendar. The resulting dossier count has to be equal to 1.
    I've created this DAX measure:
    LAST_DOSSIER_STATUS_NUMBER:=COUNTROWS(
    FILTER( VALUES(Dossier[DOSSIER_ID]);
    CALCULATE(MAX(DOSSIER[SEQUENCE])) = CALCULATE( MAX(DOSSIER[SEQUENCE]) ; all(dossier[status_id]) ) ) )
    It functions only if I select a date, but not if I select the month (february) or the year (2015).
    I've tried to use FILTER and LASTDATE functions inside the CALCULATE( MAX(DOSSIER[SEQUENCE]) ; all(dossier[status_id]) ) expression but with no results.
    Any helps for me, please? Thanks

    Hi Imke,
    for a specific day I could have three status change for a specific dossier, and so I could have three same occurence for a dossier: DISTINCTCOUNT(Dossier[DOSSIER_ID]) should return 1, but respect which status? If I should put the STATUS_ID on the rows of
    a pivot table I could see 1 for the created status, 1 for the active status and 1 for the closed status. The goal is to show only 1 value for the closed status, that is the last status assumed for the specific dossier.
    For the data posted next:
    when I select 11/02/2015 as date I've the desired behaviour
    but when I select 2015 as year I don't have the wanted behaviour (see dossier_id 1 and dossier_id 4):
    The desired behaviour is to see for dossier_id 1 only status_id 4 and not status_id 2, for dossier_id 4 only status_id 4.
    As row labels I've dossier_id and status_id, as filter I've a Time Hierarchy (date, month, year) built on a calendar table.
    Thanks

Maybe you are looking for