Found 4219 Articles for MySQLi

Can we alter order of columns in MySQL?

AmitDiwan
Updated on 18-Dec-2019 05:38:19

195 Views

Yes, we can change the order of columns. This can be done using ALTER command and AFTER to set the new order of an individual column. Let us first create a table −mysql> create table DemoTable    -> (    -> `Student_Key_Age` int,    -> `Student_Key_Name` varchar(20),    -> `Student_Key_CountryName` varchar(20)    -> ); Query OK, 0 rows affected (0.64 sec)Following is the query to alter order of columns −mysql> alter table DemoTable modify column `Student_Key_Age` int after `Student_Key_Name`; Query OK, 0 rows affected (1.15 sec) Records: 0 Duplicates: 0 Warnings: 0Let us check the table description once again −mysql> ... Read More

Display records with conditions set using if statement in UPDATE statement with MySQL

AmitDiwan
Updated on 18-Dec-2019 05:33:21

99 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentMarks int,    -> Status varchar(20)    -> ); Query OK, 0 rows affected (0.97 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName, StudentMarks) values('Chris', 79); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentName, StudentMarks) values('David', 59); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentName, StudentMarks) values('Bob', 60); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable(StudentName, StudentMarks) values('Mike', ... Read More

Perform custom sorting in MySQL

AmitDiwan
Updated on 18-Dec-2019 05:30:16

233 Views

To perform custom sorting in MySQL, use ORDER BY FIELD(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command:mysql> insert into DemoTable values(101); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(103); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(102); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(105); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement ... Read More

Concatenate the column values with separate text in MySQL and display in a single column

AmitDiwan
Updated on 18-Dec-2019 05:26:48

294 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.93 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(101, 'Chris'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(103, 'Robert'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output−+------+--------+ |   Id | Name ... Read More

Update MySQL column based on email address?

AmitDiwan
Updated on 18-Dec-2019 05:24:17

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> EmailAddress varchar(20),    -> Score int    -> ); Query OK, 0 rows affected (1.05 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris@gmail.com', 67); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Robert@gmail.com', 57); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David@gmail.com', 98); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------------+-------+ | EmailAddress     ... Read More

MySQL query to remove trailing spaces

AmitDiwan
Updated on 18-Dec-2019 05:22:21

197 Views

To remove trailing space, use RTRIM() in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> FirstName varchar(50)    -> ); Query OK, 0 rows affected (1.38 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John '); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris '); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values(' David '); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.17 sec)Display all records from the table ... Read More

Delete only some rows from a table based on a condition in MySQL

AmitDiwan
Updated on 18-Dec-2019 05:20:41

358 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command:Insert some records in the table using insert command: mysql> insert into DemoTable values(100, 'Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(101, 'Chris'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(102, 'David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(103, 'Sam'); Query OK, 1 row affected (0.08 sec) mysql> ... Read More

How to update MySQL table storage engine

AmitDiwan
Updated on 18-Dec-2019 05:12:31

193 Views

To update MySQL table engine, following the below syntax −Syntaxalter table yourTableName ENGINE=InnoDB;Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentAge int,    -> StudentCountryName varchar(20)    -> )ENGINE=MyISAM, AUTO_INCREMENT=101; Query OK, 0 rows affected (0.18 sec)Let us check the description of table −mysql> show create table DemoTable;This will produce the following output −+---------------+-----------------------------------------------------------------------------------------+ | Table         | Create Table                                   ... Read More

Order VARCHAR records with string and numbers in MySQL

AmitDiwan
Updated on 26-Feb-2020 05:52:01

381 Views

For this, use ORDER BY clause. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentCode varchar(20)    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('101J'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('100A'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('100B'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('101S'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('103M'); Query OK, 1 row affected (0.15 ... Read More

Fetch maximum individual marks for a student with marks1 and marks2 records in MySQL?

AmitDiwan
Updated on 18-Dec-2019 05:05:52

207 Views

For this, use MAX() along with GROUP BY clause. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentEmailId varchar(20),    -> Marks1 int,    -> Marks2 int -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John@gmail.com', 45, 32); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('John@gmail.com', 32, 45); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values('Carol@gmail.com', 32, 45); Query OK, 1 row affected (1.64 sec) mysql> insert into DemoTable values('David@gmail.com', 45, 32); ... Read More

Advertisements