Found 4381 Articles for MySQL

Count the number of occurrences of a string in a VARCHAR field in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

5K+ Views

To count the number of occurrences of a string in a VARCHAR, we can use the logic of subtraction with length. First, we will create a table with the help of create command. mysql> create table StringOccurrenceDemo -> ( -> Cases varchar(100), -> StringValue varchar(500) -> ); Query OK, 0 rows affected (0.56 sec) After executing the above table, we will insert records into the table. The query is as follows − mysql> insert into StringOccurrenceDemo values('First', 'This is MySQL Demo and MySQL is ... Read More

MySQL status in terms of active or total connections?

George John
Updated on 30-Jul-2019 22:30:23

8K+ Views

The active or total connection can be known with the help of threads_connected variable. The variable tells about the number of currently open connections. The query is as follows − mysql> show status where `variable_name` = 'Threads_connected'; Here is the output. +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | Threads_connected | 1 | +-------------------+-------+ 1 row in set (0.06 sec) We can check the same with the help of show command. The query is as follows − mysql> show processlist; Here is the output. +----+-----------------+-----------------+----------+---------+--------+------------------------+------------------+ ... Read More

What is the MySQL JDBC driver connection string?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

495 Views

The MySQL JDBC connection string look like this − Class.forName("com.mysql.jdbc.Driver"); Above, Driver is an interface. Whenever your JDBC is running outside an application server, then the class DriverManager establish the connection. The DriverManager class is as follows − conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/yourdatabaseName",”yourRootName","yourPassword"); Now, I am applying the above connection string to connect Java to MySQL database. The code is as follows. The following is the output that shows that MySQL connection with Java is successful.

Find rows that have the same value on a column in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

3K+ Views

First, we will create a table and insert some values into the table. Let us create a table. mysql> create table RowValueDemo -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.69 sec) Insert records using the insert command. We have added duplicate values as well for our example. mysql> insert into RowValueDemo values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into RowValueDemo values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into RowValueDemo values('Carol'); Query OK, 1 row affected ... Read More

length() vs char_length() in MySQL?

Ankith Reddy
Updated on 27-Jun-2020 07:39:24

1K+ Views

The char_length() can be used to display the length of a string. Let us see an example to get the length of the string included as a parameter.mysql> select char_length('John');The following is the output.+---------------------+ | char_length('John') | +---------------------+ | 4 | +---------------------+ 1 row in set (0.00 sec)The length() function can be used to display the length of string measured in bytes. In many cases characters and bytes gives the same length.Here is an example of length()mysql> select length('Tim'); The following is ... Read More

Get the new record key ID from MySQL insert query?

George John
Updated on 30-Jul-2019 22:30:23

628 Views

We can get new record key with the help of LAST_INSERT_ID() function from MySQL. First, we will create a table and for inserting record, we will use LAST_INSERT_ID(). Let us create a table with the help of create command. The query is as follows − mysql> create table LastInsertRecordIdDemo -> ( -> id int auto_increment, -> value varchar(100), -> primary key(id) -> ); Query OK, 0 rows affected (0.52 sec) After creating a table, we will insert records and set it using LAST_INSERT_ID() ... Read More

How to subtract 10 days from the current datetime in MySQL?

George John
Updated on 30-Jul-2019 22:30:23

3K+ Views

Firstly, let us get the current datetime with the help of the now() function. mysql> select now(); The following is the output. +---------------------+ | now()               | +---------------------+ | 2018-11-01 19:55:56 | +---------------------+ 1 row in set (0.00 sec) Syntax to subtract 10 days with the help of DATE_SUB() select DATE_SUB(now(),interval integer_value day ); Applying the above syntax to subtract 10 days from the current datetime. mysql> select DATE_SUB(now(),interval 10 day); Here is the output. +---------------------------------+ | DATE_SUB(now(),interval 10 day) | +---------------------------------+ | 2018-10-22 19:56:07             | +---------------------------------+ 1 row in set (0.00 sec)

How can I tell when a MySQL table was last updated?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

10K+ Views

We can know that with the help of the column name ‘UPDATED_TIME’ using information_schema.tables with WHERE clause. Let us first create a table for our example. mysql> create table MyISAMTableDemo   -> (   -> id int   -> ); Query OK, 0 rows affected (0.56 sec) Inserting some records into table. mysql> insert into MyISAMTableDemo values(1); Query OK, 1 row affected (0.72 sec) mysql> insert into MyISAMTableDemo values(2); Query OK, 1 row affected (0.16 sec) Syntax to know the last updated time. SELECT UPDATE_TIME FROM   information_schema.tables WHERE  TABLE_SCHEMA = 'yourDatabaseName' AND TABLE_NAME = ... Read More

How do I show the schema of a table in a MySQL database?

Arjun Thakur
Updated on 01-Sep-2023 02:31:23

101K+ Views

To show the schema, we can use the DESC command. This gives the description about the table structure. The following is the syntax. DESCRIBE yourDatabasename.yourTableName; Let us implement the above syntax. mysql> DESCRIBE business.student; The following is the output. +-------+--------------+------+-----+---------+-------+ | Field | Type         | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id    | int(11)      | YES | MUL | NULL    | | | Name  | varchar(100) | YES  | MUL | NULL |  | +-------+--------------+------+-----+---------+-------+ 2 rows in set (0.05 ... Read More

How do I check to see if a value is an integer in MySQL?

George John
Updated on 30-Jul-2019 22:30:23

13K+ Views

To check if the given value is a string or not ,we use the cast() function. If the value is not numeric then it returns 0, otherwise it will return the numeric value. In this way, we can check whether the value is an integer or not. Case 1 − Checking for a string with integers mysql> select cast('John123456' AS UNSIGNED); The following is the output. It shows that the value is not numeric, therefore 0 is returned. +--------------------------------+ | cast('John123456' AS UNSIGNED) | +--------------------------------+ |                              0 | +--------------------------------+ 1 row in set, 1 warning (0.00 sec) ... Read More

Advertisements