I want to show the result of rows affected after update, insert, or delete in mysql. I have put
DELETE FROM A WHERE ID='1';
SELECT ROW_COUNT();
With the ROW_COUNT the last statement, but the result show me is 0.
If you want to know number of rows affected by delete query in PHPMYADMIN then by running your query it will show you the result see below screenshot :
As #Flash Thunder said PHPmyadmin does not allow multiple queries sent at once
If you want to see the affected rows then you can also write a script using PHP which will exceute you sql query and returns the number of affected rows
Just to be clear.
phpMyAdmin is written in PHP, and PHP does not allow multiple queries sent at once... if you are sending two queries separately, second query is on new connection, so it has no access to previous query information. That's why SELECT ROW_COUNT(); returns 0.
But by default phpMyAdmin returns affected rows count in information after query. It probably uses mysql(i)_affected_rows() function.
FOUND_ROWS() returns number of tables in database when there was no previous query.
mysql> use hunting;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select found_rows();
+--------------+
| found_rows() |
+--------------+
| 24 |
+--------------+
1 row in set (0.00 sec)
mysql> show tables;
(...)
24 rows in set (0.00 sec)
Related
When I execute a mysql procedure on the console, this always show two times but I don't know what time each thing refers to.
Thanks in advance!
Your procedure must do a SELECT followed by some non-SELECT.
Here's a demo:
mysql> delimiter ;;
mysql> create procedure p() begin select 123; do 123+456; end;;
mysql> delimiter ;
mysql> call p;
+-----+
| 123 |
+-----+
| 123 |
+-----+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
The two times reported are for the two SQL statements run in the procedure.
The first statement returned a row.
The second statement did not return a row, but still had to execute, therefore it reported its time.
Statements that have a result set include SELECT and SHOW.
All other statements have no result set, but may affect rows. Like if an UPDATE changes rows, it will report it affected some number of rows, which are the rows it changed.
The quick example I showed in my demo only uses the DO statement, which evaluates an expression but doesn't change any data or return any data.
mysql> select * from users where id=0;
Empty set (0.00 sec)
mysql> select * from users where id=0 and sleep(5);
Empty set (0.00 sec)
mysql> select * from users where id=0 and benchmark(1000000,sha1(1));
Empty set (0.39 sec)
mysql> select * from users where id=0 xor sleep(5);
Empty set (1 min 5.02 sec)
when id=0, the user doesn't exist. why the second query doesn't sleep 5s? when I use benchmark(1000000,sha1(1)) or xor sleep(5), why it will sleep?
Thanks.
This is called "short-circuiting" and it is considered a feature of pretty much all databases -- and almost all programming languages.
Logic that does not need to be executed is not executed. It has nothing to do with sleep(). It is simply desirable to stop executing code when the engine already knows the answer.
I do not think that forcing a sleep() within a single query is a good idea. SQL Queries are not intended to be procedural. They describe the result set and the query engine generates the appropriate code. sleep() is not in the scope of SQL result sets.
I would recommend that -- if a sleep() really is necessary -- that you do it with a separate step in your scripts.
When I try to create a database which already exists,
CREATE DATABASE IF NOT EXISTS test;
Query OK, 1 row affected (0.00 sec)
CREATE DATABASE IF NOT EXISTS test;
Query OK, 1 row affected, 1 warning (0.00 sec)
Why does it show 1 row affected message second time , even though it is not creating a new database with the same name?
Although the CREATE DATABASE IF NOT EXISTS test; command won't directly modify rows in an exiting instance of the test database, it will affect the actual details stored internally in the mysql database, or possibly in one of the derived meta views, like the information_schema or performance_schema etc.
The reported Query OK, 1 row affected (0.00 sec) is referring to a row in one of these internal data constructs. When you reissue the CREATE DATABASE command, and it fails gracefully thanks to the IF NOT EXISTS clause, it is still likely to store meta-data internally, maybe an accumulating field that counts warnings or similar, or even just a 'last acted on' timestamp against this database's row. In any case the stored data in this record is changed, and is reflected as an 'affected' row.
Is it possible to get the duplicate count when executing MySQL "INSERT IGNORE" statement via JDBC?
For example, when I execute an INSERT IGNORE statement on the mysql command line, and there are duplicates I get something like
Query OK, 0 rows affected (0.02 sec)
Records: 1 Duplicates: 1 Warnings: 0
Note where it says "Duplicates: 1", indicating that there were duplicates that were ignored.
Is it possible to get the same information when executing the query via JDBC?
Thanks.
I believe you can retrieve this by issuing SHOW WARNINGS after your insert.
http://dev.mysql.com/doc/refman/5.0/en/show-warnings.html
You can use the ROW_COUNT() and FOUND_ROWS() functions to determine the number of inserted rows and duplicates when doing INSERT IGNORE.
For Example :
SELECT ROW_COUNT(), FOUND_ROWS()
INTO myRowCount, myFoundRows;
myInsertedRows = myRowCount;
myDuplicateRows = myFoundRows - myInsertedRows;
COMMIT;
You can get the rows affected value via JDBC. Just subtract it from the number of rows you inserted.
Bear in mind that rows affected also includes rows that are indirectly affected by the query. So, if there are any triggers that affect other rows, rows affected will include those rows as well.
See: http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#executeUpdate()
I'm trying to find out whether a delete query executed properly. I know how many rows its supposed to delete, and I'd like to check that the number that were deleted, matches what's expected, from within the query itself.
How do I find out the number of rows that I just deleted, from within a SQL query running against MySQL? Separate statements are fine, as long as I can bundle them into one query.
see MySQL ROW_COUNT function here http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_row-count
this is the query what you want,
SELECT ROW_COUNT()
mysql> DELETE FROM t WHERE i IN(1,2);
Query OK, 2 rows affected (0.00 sec)
mysql> SELECT ROW_COUNT() as DelRowCount;
+-------------+
| DelRowCount |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)