Found 4219 Articles for MySQLi

How to store time created in a MySQL table?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

137 Views

You can use DEFAULT CURRENT_TIMESTAMP. Keep in mind that it will work only at the time of insertion. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Arrivaltime TIMESTAMP DEFAULT CURRENT_TIMESTAMP    ); Query OK, 0 rows affected (0.31 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Arrivaltime) values('2018-01-31 10:34:56'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable(Arrivaltime) values('2019-01-31 11:10:12'); Query OK, 1 row affected (0.04 sec)Display ... Read More

What is the MySQL alias shorthand?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

198 Views

You need to give name explicitly or you can remove AS command. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20)    ); Query OK, 0 rows affected (0.21 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(Name) values('Larry'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.06 sec)Display all records from the table using select statement −mysql> select ... Read More

How to fetch only N rows at a time in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

560 Views

To fetch only N rows at a time, you can use LIMIT operator. Following is the syntax −select *from yourTableNameLIMIT 0, N;Let us first create a table −mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20) ); Query OK, 0 rows affected (0.25 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(Name) values('Larry'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Name) values('Bob'); ... Read More

Get the sum of a column in all MySQL rows?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

168 Views

Use aggregate function SUM() to get the sum of a column in al rows. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Amount int    ); Query OK, 0 rows affected (0.20 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Amount) values(50); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(Amount) values(60); Query OK, 1 row affected (0.04 sec) mysql> insert into DemoTable(Amount) values(70); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> ... Read More

How to insert only those records that does not exist in a MySQL table?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

113 Views

To insert when a record does not exist, set the column as UNIQUE INDEX. Let us first create a table −mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) ); Query OK, 0 rows affected (0.30 sec)Following is the query to create unique index to insert record which does not exist in the ‘FirstName’ column −mysql> CREATE UNIQUE INDEX index_on_FirstName ON DemoTable(FirstName); Query OK, 0 rows affected (0.56 sec) Records: 0 Duplicates: 0 Warnings: 0Insert some records in the table using insert command. ... Read More

Adding new column after a specific column and defining a default in MySQL?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

1K+ Views

You need to follow some steps to add a new column after a specific column and defining default value. In order to achieve this, you need to use ALTER command. Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentFirstName varchar(20),    StudentAge int,    StudentCountryName varchar(100)    ); Query OK, 0 rows affected (0.21 sec)Let us check the description of table −mysql> desc DemoTable;This will produce the following output −+--------------------+--------------+------+-----+---------+----------------+ | Field              | Type         | Null | ... Read More

How to use MySQL CASE statement while using UPDATE Query?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

449 Views

For using MySQL CASE statement while using UPDATE Query, you can use CASE statement. Let us first create a table −mysql> create table DemoTable    (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserScore int    ); Query OK, 0 rows affected (0.29 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(UserScore) values(100); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(UserScore) values(110); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(UserScore) values(120); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable(UserScore) values(200); Query OK, 1 ... Read More

MySQL query to select a row which contains same number in a column with set of numbers separated by comma?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

108 Views

You need to use FIND_IN_SET() for this. Let us first create a table −mysql> create table DemoTable ( CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY, CustomerName varchar(20), CustomerAllProductPrice text ); Query OK, 0 rows affected (0.30 sec)Insert some records in the table using insert command. Here, we are inserting numbers separated by comma −mysql> insert into DemoTable(CustomerName, CustomerAllProductPrice) values('Chris', '245, 345, 678, 90, 45, 56, 78'); Query OK, 1 row affected (0.03 sec) mysql> insert into DemoTable(CustomerName, CustomerAllProductPrice) values('Chris', '98, 99, 90, 56, 77'); ... Read More

Performing mathematical operations in MySQL IF then ELSE is possible?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

197 Views

For performing mathematical operations and working with conditions, you can consider CASE statement. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    FruitName varchar(100),    FruitPrice int    ); Query OK, 0 rows affected (0.26 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(FruitName, FruitPrice) values('Orange', 250); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(FruitName, FruitPrice) values('Banana', 100); Query OK, 1 row affected (0.05 sec) mysql> insert into DemoTable(FruitName, FruitPrice) values('Apple', 150); Query OK, 1 row affected (0.05 sec) ... Read More

MySQL system variable table_type doesn't work?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

58 Views

The variable table_type doesn’t work since this variable is deprecated as of MySQL 5.5.3. Use default_storage_engine instead. Following is the syntax −SET default_storage_engine = yourTableEngine;The table engine name may be InnoDB or MyISAM. Here, we will set engine type to MyISAM −mysql> SET default_storage_engine=MyISAM; Query OK,  0 rows affected (0.00 sec)Let us create a table.mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY ); Query OK,  0 rows affected (0.40 sec)Now check the engine type of above table −mysql> SHOW TABLE STATUS WHERE Name = 'DemoTable';This will produce the following output −+--------------+--------+---------+------------+------+----------------+-------------+------------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+ | Name | Engine | ... Read More

Advertisements