Found 4219 Articles for MySQLi

Append wildcards in SELECT with MySQL?

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

89 Views

For appending, use the concept of concat(). The syntax is as follows −select *from yourTableName where yourColumnName like concat('%', yourValue, '%');Let us create a table −mysql> create table demo48 -> ( −> id int not null auto_increment primary key, −> name varchar(20) −> ); Query OK, 0 rows affected (0.70 sec)Insert some records into the table with the help of insert command −mysql> insert into demo48(name) values('John Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo48(name) values('John Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo48(name) values('Adam Smith'); Query OK, 1 row ... Read More

What is the MySQL syntax error in this query – Creating a table with reserved keyword?

AmitDiwan
Updated on 19-Nov-2020 13:07:15

401 Views

Let’s say we tried creating a table with name “groups”, which is a reserved keyword in MySQL You cannot use “groups” because groups is a reserved keyword in MySQL.Following error occurred while creating a table with name “groups” −mysql> create table groups −> ( −> id int, −> name varchar(40) −> ); ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups ( id int, name varchar(40) )' at line 1In order to create a table with reserved keyword, you need ... Read More

Remove specific fields/ rows and show other records in MySQL?

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

114 Views

For this, use CASE WHEN statement in MySQL. Let us create a table −mysql> create table demo47 −> ( −> first_name varchar(20), −> last_name varchar(20) −> ); Query OK, 0 rows affected (1.57 sec)Insert some records into the table with the help of insert command −mysql> insert into demo47 values('John', 'Smith'); Query OK, 1 row affected (0.18 sec) mysql> insert into demo47 values('David', 'Miller'); Query OK, 1 row affected (0.11 sec) mysql> insert into demo47 values('John', 'Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo47 values('Chris', 'Brown'); Query OK, 1 row affected (0.12 sec)Display ... Read More

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

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

215 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

Advertisements