Select Rows Containing a String in a Specific Column with MATCH and AGAINST in MySQL

AmitDiwan
Updated on 24-Dec-2019 07:49:58

243 Views

Let us first create a table −mysql> create table DemoTable1833      (      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Alter table −Mysql> alter table DemoTable1833 ADD FULLTEXT(Name); Query OK, 0 rows affected, 1 warning (0.00 sec) Records: 0  Duplicates: 0  Warnings: 1Insert some records in the table using insert command −mysql> insert into DemoTable1833 values('John Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1833 values('Adam Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1833 values('Chris Brown'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1833 ... Read More

Insert JSON into a MySQL Table

AmitDiwan
Updated on 24-Dec-2019 07:48:58

2K+ Views

Let us create a table and set a column value with type JSONmysql> create table DemoTable1832      (      ListOfNames JSON      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1832(ListOfNames) values('["Sam", "Mike", "Carol"]'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1832(ListOfNames) values('["David", "Bob"]'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1832(ListOfNames) values('["Adam", "John", "Sam"]'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1832; This will produce the following ... Read More

Set Custom Auto Increment with Zerofill in MySQL

AmitDiwan
Updated on 24-Dec-2019 07:47:56

796 Views

Let us first create a table. Here. We have set UserId column with ZEROFILL and AUTO_INCREMENTmysql> create table DemoTable1831      (      UserId int(7) zerofill auto_increment,      PRIMARY KEY(UserId)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1831 values(101); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1831 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1831 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1831 values(); Query OK, 1 row affected (0.00 sec)Display all records ... Read More

Delete Specific Rows in a Table with MySQL

AmitDiwan
Updated on 24-Dec-2019 07:46:55

464 Views

To delete only specific rows, use MySQL NOT IN(). Let us first create a table −mysql> create table DemoTable1830      (      StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,      StudentName varchar(20)      )AUTO_INCREMENT=101; Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1830(StudentName) values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1830(StudentName) values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1830(StudentName) values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1830(StudentName) values('Sam'); Query OK, 1 row affected (0.00 ... Read More

Count Only No Values from MySQL Column

AmitDiwan
Updated on 24-Dec-2019 07:45:54

153 Views

Let us first create a table −mysql> create table DemoTable1829      (      Name varchar(20),      isTopper ENUM('YES', 'NO')      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1829 values('Chris', 'yes'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1829 values('David', 'yes'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1829 values('Mike', 'no'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1829 values('David', 'yes'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement ... Read More

Add a Set of Elements in One Cell with MySQL

AmitDiwan
Updated on 24-Dec-2019 07:44:46

83 Views

To add a set of elements in a single cell, use the concept of JSON. Let us first create a table −mysql> create table DemoTable1828      (      EmployeeId int,      EmployeeRecords JSON      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1828 values(1, '[{"EmployeeName":"Chris", "EmployeeAge":29}, {"EmployeeName":"David", "EmployeeAge":27}]'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1828 values(2, '[{"EmployeeName":"John", "EmployeeAge":36}, {"EmployeeName":"Mike", "EmployeeAge":32}]'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1828; This ... Read More

Perform Mathematical Operations in a MySQL Stored Procedure

AmitDiwan
Updated on 24-Dec-2019 07:41:15

835 Views

Let us create a stored procedure. Here, we are calculating amount*quantity i.e. implementing mathematical operations −mysql> delimiter // mysql> create procedure calculation_proc(amount int,quantity int)      begin      select amount,quantity,(amount*quantity) as Total;      end      // Query OK, 0 rows affected (0.00 sec) mysql> delimiter ;Now you can call a stored procedure using call command −mysql> call calculation_proc(250,3);This will produce the following output −+--------+----------+-------+ | amount | quantity | Total | +--------+----------+-------+ |    250 |        3 |   750 | +--------+----------+-------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)

Search for Character in a MySQL Table

AmitDiwan
Updated on 24-Dec-2019 07:40:06

509 Views

To search for ^ character, use the LIKE operator as in the below syntax −select table_schema, table_name, column_name  from information_schema.columns  where column_name like '%^%';Let us first create a table −mysql> create table DemoTable1826      (      `^` varchar(20),      Name varchar(20),      `^Age` int      ); Query OK, 0 rows affected (0.00 sec)Here is the query to search for ^ character in a MySQL tablemysql> select table_schema, table_name, column_name      from information_schema.columns      where column_name like '%^%';This will produce the following output −+--------------+---------------+-------------+ | TABLE_SCHEMA | TABLE_NAME    | COLUMN_NAME | +--------------+---------------+-------------+ | ... Read More

Display Only Not Null Values from a Column in MySQL

AmitDiwan
Updated on 24-Dec-2019 07:38:49

538 Views

For this, you can use IS NOT NULL property. Let us first create a table −mysql> create table DemoTable1      (      DueDate date      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values('2019-09-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1 values('2019-11-10'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1; This will produce the following output −+------------+ ... Read More

Maximize Value of arr[i] - arr[j] in an Array in C++

Narendra Kumar
Updated on 24-Dec-2019 07:21:26

240 Views

Problem statementGiven an array, arr[] find the maximum value of (arr[i] – i) – (arr[j] – j) where i is not equal to j. Where i and j vary from 0 to n-1 and n is the size of input array arr[].If the input array is {7, 5, 10, 2, 3} then we can obtain 9 maximum value as follows−(element 10 – index 2) - (element 2 – index 3) (10 – 2) – (2 – 3) = 8 – (-1) = 9Algorithm1. Find maximum value of (arr[i] – i) in whole array. 2. Find minimum value of (arr[i] – ... Read More

Advertisements