Found 4219 Articles for MySQLi

Sort by date & time in descending order in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

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

Calculate average of numbers in a column MySQL query?

Jennifer Nicholas
Updated on 25-Jun-2020 10:45:08

840 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

How to reset the primary key of a table in mysql?

Anvi Jain
Updated on 29-Jun-2020 07:40:36

854 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

Maintaining order in MySQL “IN” query?

Vrundesha Joshi
Updated on 29-Jun-2020 07:42:46

557 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

Add a temporary column with a value in MySQL?

Rishi Rathor
Updated on 25-Jun-2020 10:48:13

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

How to find the highest number in a column?

Jennifer Nicholas
Updated on 29-Jun-2020 07:44:16

76 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

Simple way to toggle a value of an int field in MySQL

Anvi Jain
Updated on 29-Jun-2020 07:52:06

304 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

How to subtract 3 hours from a datetime in MySQL?

Vrundesha Joshi
Updated on 29-Jun-2020 07:53:22

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

How to resolve the ERROR 1115 (42000): Unknown character set: 'utf8mb4'?

Jennifer Nicholas
Updated on 29-Jun-2020 08:03:49

4K+ 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

Avoid placing password on command line with MySQL Utilities?

Anvi Jain
Updated on 29-Jun-2020 08:04:20

140 Views

First you need to reach the location of “my.cnf” with the help of below query for MySQL Utilities. The query is as follows −mysql> select @@datadir;The following is the output that display where “my.conf” is −+---------------------------------------------+ | @@datadir                                   | +---------------------------------------------+ | C:\ProgramData\MySQL\MySQL Server 8.0\Data\ | +---------------------------------------------+ 1 row in set (0.00 sec)Follow the above path in order to open the “my.cnf” file. The snapshot of “my.cnf” location is as follows −Now you can put the below MySQL Utilities in my.cnf which is as ... Read More

Advertisements