Found 4378 Articles for MySQL

How to SELECT all values from a table only once if they're duplicated?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

6K+ Views

You can use distinct keyword to select all values from a table only once if they are repeated.The syntax is as followsselect distinct yourColumnName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows.mysql> create table displayOnlyDistinctValue    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserName varchar(100),    -> UserAge int    -> ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command.The query is as follows.mysql> insert into displayOnlyDistinctValue(UserName, UserAge) values('Larry', 23); Query OK, 1 row affected ... Read More

MySQL query to select all entries from a particular month

Naveen Singh
Updated on 30-Jul-2019 22:30:25

17K+ Views

To select all entries from a particular month in MySQL, use the monthname() or month() function.The syntax is as follows.select *from yourTableName where monthname(yourColumnName)='yourMonthName';To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table selectAllEntriesDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.63 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into selectAllEntriesDemo(ShippingDate) values('2019-01-21'); Query OK, 1 row affected ... Read More

How to alter a MySQL Column from varchar(30) to varchar(100)?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

496 Views

You need to use ALTER TABLE command along with MODIFYThe syntax is as followsALTER TABLE yourTableName MODIFY COLUMN yourColumnName varchar(100) NOT NULL;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table syntaxOfAlterCommandDemo    -> (    -> UserId int,    -> UserName varchar(30),    -> UserAge int,    -> UserCityName varchar(50)    -> ); Query OK, 0 rows affected (0.51 sec)Let us check the description of the table.The query is as followsmysql> desc syntaxOfAlterCommandDemo;The following is the output+--------------+-------------+------+-----+---------+-------+ | Field | Type ... Read More

MySQL replace values in a table?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

188 Views

To replace values in a table, use the CASE statement. To understand the concept, let us create a table. The query to create a table is as followsmysql> create table replaceValueDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(10),    -> isGreaterThan18 varchar(10)    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into replaceValueDemo(Name, isGreaterThan18) values('John', 'YES'); Query OK, 1 row affected (0.24 sec) mysql> insert into replaceValueDemo(Name, isGreaterThan18) values('Carol', 'NO'); Query OK, 1 row affected (0.16 sec) ... Read More

Retrieve time from MySQL as HH:MM format?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

2K+ Views

To retrieve time as HH:MM format, use the DATE_FORMAT() function. To understand the function and retrieve time, let us create a table.The query to create a table is as followsmysql> create table retrieveTimeDemo    -> (    -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> UserArrivalTime datetime    -> ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into retrieveTimeDemo(UserArrivalTime) values('2013-01-21 13:45:34'); Query OK, 1 row affected (0.16 sec) mysql> insert into retrieveTimeDemo(UserArrivalTime) values('2014-11-25 11:40:56'); Query OK, 1 row affected (0.11 sec) mysql> insert into retrieveTimeDemo(UserArrivalTime) ... Read More

Get MySQL maximum value from 3 different columns?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

6K+ Views

To get the maximum value from three different columns, use the GREATEST() function.The syntax is as followsSELECT GREATEST(yourColumnName1, yourColumnName2, yourColumnName3) AS anyAliasName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table MaxOfThreeColumnsDemo    -> (    -> First int,    -> Second int,    -> Third int    -> ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into MaxOfThreeColumnsDemo values(30, 90, 60); Query OK, 1 row affected (0.16 sec) mysql> insert into MaxOfThreeColumnsDemo values(100, ... Read More

How to restart MySQL server?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

1K+ Views

Restart the MySQL Server with the help of restart command.The syntax is as followsRestartLet us first check the MySQL version.The query is as followsSELECT version();Now, implement the above command in order to restart the MySQL Server.The query is as followsmysql> restart; Query OK, 0 rows affected (0.00 sec)Case 1Now, MySQL Server is being restarted. If you try to perform any query during the restart command, you will get an error.The query is as followsmysql> show databases; ERROR 2013 (HY000): Lost connection to MySQL server during queryCase 2If the MySQL restart process is complete, then a new connection id will be ... Read More

Set auto increment initial value for MySQL table using ALTER command

Naveen Singh
Updated on 30-Jul-2019 22:30:25

556 Views

To set auto increment initial value for MySQL table, use ALTER command. The first step would bealter table yourTableName modify yourColumnName int NOT NULL AUTO_INCREMENT PRIMARY KEY, add index(yourColumnName);The second step is as followsalter table yourTableName AUTO_INCREMENT=yourStartingValue;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table setAutoIncrementDemo    -> (    -> UserId int,    -> UserName varchar(20)    -> ); Query OK, 0 rows affected (0.75 sec)Now implement the above two steps to set auto increment initial value for MySQL table.Step 1 -The query is as followsmysql> alter ... Read More

How to add +1 to existing MySQL values?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

580 Views

Let us see an example and create a table first.mysql> create table Add1ToExistingValue    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into Add1ToExistingValue values(10); Query OK,  1 row affected (0.12 sec) mysql> insert into Add1ToExistingValue values(13); Query OK,  1 row affected (0.15 sec) mysql> insert into Add1ToExistingValue values(15); Query OK,  1 row affected (0.13 sec) mysql> insert into Add1ToExistingValue values(16); Query OK,  1 row affected (0.14 sec) mysql> insert into Add1ToExistingValue values(20); Query OK,  1 row affected (0.16 sec) mysql> insert into Add1ToExistingValue values(40); Query OK,  1 row affected (0.15 sec) mysql> insert into Add1ToExistingValue values(50); Query OK,  1 row affected (0.11 sec) mysql> insert into Add1ToExistingValue values(55); Query OK,  1 row affected (0.17 sec) mysql> insert into Add1ToExistingValue values(56); Query OK,  1 row affected (0.17 sec)Display all records from the table using select statement.The query is as followsmysql> select *from Add1ToExistingValue;The following is the output+-------+ | Value | +-------+ | 10   ... Read More

Resolve the error Column count doesn’t match value count in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:25

635 Views

This type of error occurs when number of columns does not match whenever you are inserting records in the destination table. For a demo example, let us create a tablemysql> create table errorDemo    -> (    -> User_Id int NOT NULL AUTO_INCREMENT,    -> User_Name varchar(20),    -> PRIMARY KEY(User_Id)    -> ); Query OK, 0 rows affected (0.47 sec)The error is as followsmysql> insert into errorDemo values('John'); ERROR 1136 (21S01): Column count doesn't match value count at row 1To avoid this type of error, you need to use the following syntaxinsert into yourTableName(yourColumnName1, yourColumnName2, ...N)values(yourValue1, yourValue2, ....N);Insert some ... Read More

Advertisements