Found 4378 Articles for MySQL

How to convert MM/YY to YYYY-MM-DD with a specific day in MySQL?

AmitDiwan
Updated on 19-Nov-2020 13:03:05

213 Views

To convert, use STR_TO_DATE(), as in the below syntax. Concatenate the day value with CONCAT() −select str_to_date(concat('yourDateValue/', yourColumnName), '%d/%m/%y') as anyAliasName from yourTableName;Let us create a table −mysql> create table demo46 −> ( −> id int not null auto_increment primary key, −> short_date varchar(20) −> ); Query OK, 0 rows affected (0.60 sec)Insert some records into the table with the help of insert command −mysql> insert into demo46(short_date) values('09/18'); Query OK, 1 row affected (0.08 sec) mysql> insert into demo46(short_date) values('12/20'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo46(short_date) values('11/20'); Query OK, 1 row affected ... Read More

MySQL REGEXP to fetch string + number records beginning with specific numbers?

AmitDiwan
Updated on 19-Nov-2020 13:01:41

215 Views

For this, use REGEXP and fetch records beginning with specific numbers. Following is the syntax:Select yourColumnName1, yourColumnName2 from yourTableName where yourColumnName2 REGEXP '^yourStringValue[yourNumericValue]';Let us create a table −mysql> create table demo45 -> ( −> id int not null auto_increment primary key, −> value varchar(50) −> ); Query OK, 0 rows affected (1.50 sec)Insert some records into the table with the help of insert command. We are inserting records mixed with strings and numbers i.e. “John500, “John6500”, etc −mysql> insert into demo45(value) values('John500'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo45(value) values('John1500'); Query OK, 1 row affected (0.11 ... Read More

Select highest salary in MySQL?

AmitDiwan
Updated on 19-Nov-2020 13:00:16

851 Views

For this, you can use MAX(). The syntax is as follows −select MAX(yourColumnName) AS anyAliasName from yourTableName;Let us create a table −mysql> create table demo44 −> ( −> employee_id int not null auto_increment primary key, −> employee_name varchar(20), −> employee_salary int −> ) −> ; Query OK, 0 rows affected (1.27 sec)Insert some records into the table with the help of insert command −mysql> insert into demo44(employee_name, employee_salary) values('John', 3000); Query OK, 1 row affected (0.13 sec) mysql> insert into demo44(employee_name, employee_salary) values('David', 4500); Query OK, 1 row affected (0.12 sec) mysql> insert into demo44(employee_name, employee_salary) values('Bob', 3500); ... Read More

Creating a Table in MySQL to set current date as default

AmitDiwan
Updated on 19-Nov-2020 12:58:22

8K+ Views

Following is the syntax for creating a table and adding DEFAULT constraint to set default value −CREATE TABLE yourTableName ( yourColumnName1 dataType not null , yourColumnName2 dataType default anyValue, . . . N );;Let us create a table wherein we have set “employee_joining_date” with default constraint for current date as default −mysql> create table demo43 −> ( −> employee_id int not null auto_increment primary key, −> employee_name varchar(40) not null, −> employee_status varchar(60) default "NOT JOINED", −> employee_joining_date date default(CURRENT_DATE) −> ); Query OK, 0 rows affected (0.66 sec)Insert some records into the table with the help of insert command ... Read More

SELECT a row by subtracting dates in WHERE in MySQL?

AmitDiwan
Updated on 19-Nov-2020 12:55:29

120 Views

For this, use TIMESTAMPDIFF(). Let us create a table −mysql> create table demo42 −> ( −> start_date datetime −> ); Query OK, 0 rows affected (0.77 sec)Insert some records into the table with the help of insert command −mysql> insert into demo42 values('2020-01-10 12:30:05'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo42 values('2019-02-24 10:40:45'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo42 values('2020-05-12 05:45:55'); Query OK, 1 row affected (0.17 sec) mysql> insert into demo42 values('2020-05-12 05:40:55'); Query OK, 1 row affected (0.15 sec) mysql> insert into demo42 values('2020-05-12 05:42:55'); ... Read More

MySQL regexp to display only records with strings or strings mixed with numbers. Ignore only the number records

AmitDiwan
Updated on 19-Nov-2020 12:54:06

99 Views

For this, you can use REGEXP. Following is the syntax −select yourColumnName from yourTableName where yourColumnName REGEXP '[a−zA&minu;Z]';Let us create a table −mysql> create table demo41 −> ( −> name varchar(40) −> ); Query OK, 0 rows affected (0.64 sec)Insert some records into the table with the help of insert command −mysql> insert into demo41 values('John Smith34') −> ; Query OK, 1 row affected (0.13 sec) mysql> insert into demo41 values('John Smith'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo41 values('9234John Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into demo41 values('john smith'); Query OK, ... Read More

How to select next row pagination in MySQL?

AmitDiwan
Updated on 19-Nov-2020 12:52:43

307 Views

For this, use the LIMIT concept. Let us create a table −mysql> create table demo40 −> ( −> id int not null auto_increment primary key, −> name varchar(40) −> ); Query OK, 0 rows affected (1.73 sec)Insert some records into the table with the help of insert command −mysql> insert into demo40(name) values('Chris'); Query OK, 1 row affected (0.23 sec) mysql> insert into demo40(name) values('David'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo40(name) values('Mike'); Query OK, 1 row affected (0.10 sec) mysql> insert into demo40(name) values('Sam'); Query OK, 1 row affected (0.19 sec) ... Read More

How to select rows with condition via concatenate in MySQL?

AmitDiwan
Updated on 19-Nov-2020 12:47:01

178 Views

For this, you can use CONCAT_WS(). Let us create a table −mysql> create table demo38 −> ( −> user_id int, −> user_first_name varchar(20), −> user_last_name varchar(20), −> user_date_of_birth date −> ); Query OK, 0 rows affected (1.70 sec)Insert some records into the table with the help of insert command −mysql> insert into demo38 values(10, 'John', 'Smith', '1990−10−01'); Query OK, 1 row affected (0.14 sec) mysql> insert into demo38 values(11, 'David', 'Miller', '1994−01−21'); Query OK, 1 row affected (0.13 sec) mysql> insert into demo38 values(11, 'John', 'Doe', '1992−02−01'); Query OK, 1 row affected (0.13 sec) mysql> insert into ... Read More

Using Prepared statement correctly with WHERE condition in case of any value in MySQL Java

AmitDiwan
Updated on 19-Nov-2020 12:44:45

780 Views

For this, you can use PrepareStatement in Java. Following is the syntax −String anyVariableName="select yourColumnName from yourTableName where name = ?"; PreparedStatement ps = (PreparedStatement) con.prepareStatement(yourVariableName); ps.setString(yourColumnIndex, yourValue);Let us create a table −mysql> create table demo37 −> ( −> id int not null auto_increment primary key, −> name varchar(200) −> ); Query OK, 0 rows affected (2.46 sec)Insert some records into the table with the help of insert command −mysql> insert into demo37(name) values('John'); Query OK, 1 row affected (0.09 sec) mysql> insert into demo37(name) values('Bob'); Query OK, 1 row affected (0.08 sec) mysql> insert into demo37(name) values('John'); Query OK, ... Read More

How to count the number of columns having specific value in MySQL?

AmitDiwan
Updated on 19-Nov-2020 12:41:51

225 Views

Following is the syntax −select sum(yourColumnName1+yourColumnName2+yourColumnName3...N) as `anyAliasName1`, sum(yourColumnName1 and yourColumnName2 and yourColumnName3….N) as anyAliasName from yourTableName;Let us create a table −mysql> create table demo36 −> ( −> id int not null auto_increment primary key, −> value1 int, −> value2 int, −> value3 int −> ); Query OK, 0 rows affected (1.68 sec)Insert some records into the table with the help of insert command −mysql> insert into demo36(value1, value2, value3) values(1, 0, 0); Query OK, 1 row affected (0.14 sec) mysql> insert into demo36(value1, value2, value3) values(1, 0, 1); Query OK, 1 row affected (0.11 sec) mysql> insert ... Read More

Advertisements