Using Variables with MySQL Prepare Statement

AmitDiwan
Updated on 10-Oct-2019 12:34:09

184 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FirstName varchar(20),    LastName varchar(20) ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FirstName, LastName) values('John', 'Smith'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(FirstName, LastName) values('David', 'Miller'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(FirstName, LastName) values('John', 'Doe'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(FirstName, LastName) values('Chris', 'Brown'); Query OK, 1 row affected (0.15 sec)Display all records from ... Read More

Count Occurrences of Distinct Values in MySQL

AmitDiwan
Updated on 10-Oct-2019 12:31:39

608 Views

Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected ... Read More

Get the Sum of Last 3 Digits from Column Values in MySQL

AmitDiwan
Updated on 10-Oct-2019 12:29:08

225 Views

Since we want the sum of last 3 digits, we need to use aggregate function SUM() along with RIGHT(). Let us first create a table −mysql> create table DemoTable (    Code int ); Query OK, 0 rows affected (0.77 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(5464322); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(90884); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(23455644); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(4353633); Query OK, 1 row affected (0.11 sec)Display all records from ... Read More

Update MySQL Value by Removing Separator and Numbers

AmitDiwan
Updated on 10-Oct-2019 12:27:38

545 Views

Here, let’s say you have a string with form “StringSeparatorNumber” form like John/56989. Now if you want to remove the number after separator /, then use the SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable (    StudentName varchar(100) ); Query OK, 0 rows affected (1.05 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John/56989'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('David/74674'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('Bob/45565'); Query ... Read More

Find and Replace in MySQL Column with File Path

AmitDiwan
Updated on 10-Oct-2019 12:25:27

432 Views

For thus, use MySQL REPLACE(). Let us first create a table −mysql> create table DemoTable (    FolderLocation text ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('C/ProgramFiles/AllMySQLProgram'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('C/ProgramFiles/JavaChatApplication'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('C/ProgramFiles/Main/Image.png'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------------------------------+ | FolderLocation                   ... Read More

Check for Blank String and 0 in One MySQL Query

AmitDiwan
Updated on 10-Oct-2019 12:23:19

104 Views

Yes, we can check for a blank string and 0 in a single condition. Let us first create a table −mysql> create table DemoTable (    ClientId varchar(40) ); Query OK, 0 rows affected (1.01 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('CLI-01'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('0'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('CLI-02'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values('CLI-00'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(''); Query OK, 1 ... Read More

Collapse Rows into a Comma-Delimited List with a Single MySQL Query

AmitDiwan
Updated on 10-Oct-2019 12:20:42

714 Views

To collapse rows into a comma-delimited list, use GROUP_CONCAT(). Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(40) ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris Brown'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(101, 'Adam Smith'); Query OK, 1 row affected (0.84 sec) mysql> insert into DemoTable values(101, 'John Doe'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(100, 'David Miller'); Query OK, 1 row affected (0.17 sec) mysql> insert ... Read More

MySQL SP_HELP to Display Field Types and Foreign Key Constraints

AmitDiwan
Updated on 10-Oct-2019 12:17:21

1K+ Views

In MySQL, you can achieve sp_help with the help of SHOW CREATE command.CASE 1 −For table, the syntax is as follows −SHOW CREATE TABLE yourTableName;CASE 2 −For stored procedure, the syntax is as follows −SHOW CREATE PROCEDURE yourProcedureName;Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT,    EmployeeFirstName varchar(40) NOT NULL,    EmployeeLastName varchar(40) NOT NULL,    EmployeeAge int,    EmployeeCountryName varchar(40),    EmployeeCityName varchar(40),    PRIMARY KEY(EmployeeId),    UNIQUE KEY(EmployeeFirstName, EmployeeCityName) ); Query OK, 0 rows affected (0.97 sec)Let us implement the above syntax for our table −mysql> SHOW CREATE TABLE ... Read More

MySQL Query to Split String and Return Last Word

AmitDiwan
Updated on 10-Oct-2019 12:13:15

362 Views

For this, you can use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable (    Words TEXT ); Query OK, 0 rows affected (1.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Learn With Ease'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Intro To MySQL'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('Deep Dive Using Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('C++ In Depth'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select ... Read More

Set All Columns of a MySQL Table to a Particular Value

AmitDiwan
Updated on 10-Oct-2019 12:11:12

152 Views

Let us first create a table −mysql> create table DemoTable (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(40),    ClientAge int,    ClientCountryName varchar(40) ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ClientName, ClientAge, ClientCountryName) values('Chris', 25, 'US'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(ClientName, ClientAge, ClientCountryName) values('Bob', 55, 'UK'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(ClientName, ClientAge, ClientCountryName) values('David', 45, 'AUS'); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement ... Read More

Advertisements