- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4378 Articles for MySQL
5K+ Views
You need to store the date as the complete date time rather than storing only month and year. If you declare as a datetime then you can extract the month and year using MONTH() and YEAR() function from MySQL.The syntax is as follows −select MONTH(yourDateTimeColumnName) as anyVariableName1, YEAR(yourDateTimeColumnName) as anyVariableName2 from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table OnlyMonthandYear -> ( -> DueDateTime datetime -> ); Query OK, 0 rows affected (0.56 sec)Insert date in the table using insert command. ... Read More
2K+ Views
Let us create a table to sort date and time in ascending order. The query to create a table is as follows −mysql> create table SortByDateAndTime -> ( -> UserId int, -> UserName varchar(100), -> IssueDate date, -> IssueTime time -> ); Query OK, 0 rows affected (0.60 sec)Insert the records in the table using insert command. The query is as follows −mysql> insert into SortByDateAndTime values(1, 'John', '2018-12-16', '10:30'); Query OK, 1 row affected (0.14 sec) mysql> insert into SortByDateAndTime values(2, 'Bob', '2018-12-16', '10:10'); Query OK, 1 row affected (0.14 sec) ... Read More
855 Views
Calculate the average of numbers in a column with the help of MySQL aggregate function AVG().The syntax is as follows −select avg(yourColumnName) as anyVariableName from yourTableName;To understand the above concept, let us create a table. The following is the query to create a table.mysql> create table AverageCalculateDemo −> ( −> SubjectMarks int −> ); Query OK, 0 rows affected (0.67 sec)The following is the query to insert some records into the table −mysql> insert into AverageCalculateDemo values(70); Query OK, 1 row affected (0.14 sec) mysql> insert into AverageCalculateDemo values(80); Query OK, 1 row affected ... Read More
866 Views
The reset the primary key of a table means to reset the auto_increment property to 1. The syntax is as follows to reset the primary key of a table.alter table yourTableName auto_increment = 1;To understand, let us create a table −mysql> create table ResetPrimaryKey −> ( −> Id int auto_increment, −> PRIMARY KEY(Id) −> ); Query OK, 0 rows affected (0.59 sec)Insert some records into the table. The query to insert records is as follows −mysql> insert into ResetPrimaryKey values(); Query OK, 1 row affected (0.18 sec) mysql> insert into ResetPrimaryKey values(); Query OK, 1 row affected ... Read More
565 Views
You can maintain the order in MySQL IN query with the help of field command. The syntax is as follows −select *from yourTableName anyVariableName where anyVariableName.yourColumnName in(value1, value2, ......N) order by field(anyVariableName.yourColumnName, value1, value2, ......N);To implement the above syntax let us create a table −mysql> create table OrderInDemo −> ( −> Id int, −> Name varchar(100), −> Age int −> ); Query OK, 0 rows affected (1.24 sec)Now let us insert some records in the table. The query to insert records are as follows −mysql> insert into OrderInDemo values(90, ... Read More
3K+ Views
You can add a temporary column with value with the help of the following syntax −select yourColumnName1, yourColumnName2, .....N ,yourTemporaryColumnValue as yourTemporaryColumnName from yourTableName;To add a temporary column with a value, let us create a table. The following is the query −mysql> create table TemporaryColumnWithValueDemo −> ( −> StudentId int, −> StudentName varchar(100) −> ); Query OK, 0 rows affected (0.59 sec)Inserting some records in the table. The query to insert records are as follows −mysql> insert into TemporaryColumnWithValueDemo values(101, 'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into ... Read More
79 Views
You can find the highest number in a column with the help of aggregate function MAX. The syntax is as follows −select max(yourColumnName) as anyVariableName from yourTableName;To understand the above concept, let us create a table with an int column. The following is the query to create a table.mysql> create table HighestNumberDemo −> ( −> BigNumber int −> ); Query OK, 0 rows affected (0.87 sec)Now insert some values in the table. The query to insert records are as follows −mysql> insert into HighestNumberDemo values(1234); Query OK, 1 row affected (0.43 sec) mysql> insert ... Read More
307 Views
To toggle a value of an int field, you can use update command with if(). The syntax is as follows −update yourTableName set yourColumnName = IF(yourColumnName = 0, 1, 0);To understand the above toggle syntax, create a table with some int value. The query to create a table is as follows −mysql> create table ToggleDemo −> ( −> IsOnOrOff int −> ); Query OK, 0 rows affected (0.53 sec)Let us insert int values in the table with the help of insert command. The query is as follows −mysql> insert into ToggleDemo values(1); Query OK, 1 ... Read More
3K+ Views
Subtract 3 hours from DateTime in MySQL, using any of the following ways. The first approach is as follows −Case 1 − Using DATE_ADD()select date_add(yourColumnName, interval -3 hours) from yourTableName;Case 2 − Using DATE_SUB()select date_sub(yourColumnName, interval 3 hours) from yourTableName;Firstly, use now() to get the current date-time −mysql> select now();The following is the output −+---------------------+ | now() | +---------------------+ | 2018-11-30 10:13:23 | +---------------------+ 1 row in set (0.00 sec)DATE_ADDThe query to subtract 3 hours from DateTime is as follows. With date_add, we have set a negative date −mysql> select date_add(now(), interval -3 ... Read More
5K+ Views
You will get this type of error when your MySQL version is below 5.5.3. This is because “utf8mb4” introduced in MySQL version 5.5.3.Firstly, you need to check the current version. If its less than 5.5.3, then you need to upgrade to solve the above error.Check the current version −mysql> select version();Here, our MySQL version is over 5.5.3 −+-----------+ | version() | +-----------+ | 8.0.12 | +-----------+ 1 row in set (0.00 sec)Now the same query that gave an error 1115, will display correct result. To check all character set in MySQL now, use the below query.mysql> show character set;The ... Read More