Found 4378 Articles for MySQL

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

Select the maximum for each value in a MySQL table?

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

141 Views

For this, use GROUP BY clause along with MAX(). Let us first create a table −mysql> create table DemoTable    -> (    -> CountryName varchar(20),    -> Population int    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('US', 560); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('UK', 10090); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('UK', 8794); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('US', 1090); Query OK, 1 row affected (0.21 sec)Display ... Read More

Does grant on *.* apply to databases created after the grant in MySQL?

AmitDiwan
Updated on 18-Dec-2019 05:00:47

95 Views

Yes, since this is a global privilege. Let us first create a user −mysql> CREATE USER 'Jace'@'localhost' IDENTIFIED BY 'Jace123'; Query OK, 0 rows affected (0.67 sec)Here is the query to grant for global privileges with *.*:mysql> GRANT SELECT ON *.* TO 'Jace'@'localhost'; Query OK, 0 rows affected (0.58 sec)Now you can show all grants for a user −mysql> show grants for 'Jace'@'localhost';This will produce the following output −+-------------------------------------------+ | Grants for Jace@localhost                 | +-------------------------------------------+ | GRANT SELECT ON *.* TO `Jace`@`localhost` | +-------------------------------------------+ 1 row in set (0.14 sec)

MySQL query to sort by both timestamp and enum?

AmitDiwan
Updated on 17-Dec-2019 08:06:34

74 Views

For this, you can use ORDER BY DATE(). Let us first create a table. Here, we have a column with type DATE and another with type ENUM −mysql> create table DemoTable    -> (    -> JoiningDate date,    -> Status ENUM('Good', 'Excellent', 'Bad')    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21', 'Excellent'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Bad'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Good'); Query OK, 1 row affected (0.13 sec)Display all ... Read More

Sort records with special characters like '2321/78/54-6'

AmitDiwan
Updated on 26-Feb-2020 06:01:18

97 Views

To sort, use ORDER BY SUBSTRING(). Let us first create a table −mysql> create table DemoTable    -> (    -> Value varchar(40)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2321/78/54-6')    -> ; Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2321/78/54-8'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2321/78/54-5'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2321/78/54-9'); Query OK, 1 row affected (0.21 sec)Display all records from the table using select statement ... Read More

Display only date from timestamp value in MySQL

AmitDiwan
Updated on 17-Dec-2019 08:01:13

174 Views

To display the only date from timestamp value, use the FROM_UNIXTIME() method in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> timestampValue bigint    -> ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1538332200); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(1577730600); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(1488652200); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the ... Read More

Advertisements