Found 4219 Articles for MySQLi

Create a dynamic table name from current year in MySQL like 2019

AmitDiwan
Updated on 31-Dec-2019 07:18:24

911 Views

To create a table name like year (2019), use PREPARE statement. Let us first create a table −mysql> create table DemoTable1959    (    UserName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1959 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1959 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1959 values('Bob'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1959;This will produce the following output −+----------+ | UserName ... Read More

Display auto increment user id sequence number to begin from 001 in MySQL?

AmitDiwan
Updated on 31-Dec-2019 07:14:27

607 Views

For this, use ZEROFILL and alter the table to begin from the same sequence −alter table yourTableName    change yourColumnName yourColumnName int(3) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY;To understand the above syntax, let us first create a table −mysql> create table DemoTable1958    (    UserId int,    UserName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Here is the query to alter generated sequence number to begin from 001:mysql> alter table DemoTable1958    change UserId UserId int(3) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY; Query OK, 0 rows affected (0.00 sec) Records: 0  Duplicates: 0  Warnings: 0Let ... Read More

Divide a column to get monthly salary of employees in a MySQL Query?

AmitDiwan
Updated on 31-Dec-2019 07:11:39

298 Views

Let us first create a table −mysql> create table DemoTable1957    (    EmployeeId int,    EmployeeName varchar(20),    EmployeeSalary int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1957 values(1, 'Chris', 240000); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1957 values(2, 'Bob', 120000); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1957 values(3, 'David', 180000); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1957 values(4, 'Mike', 650000); Query OK, 1 row affected (0.00 sec)Display all records from the table using ... Read More

Select a column if condition is met in MySQL to fetch records from current date and current date + 1

AmitDiwan
Updated on 31-Dec-2019 07:04:51

432 Views

Let us first get the current date −mysql> select curdate();This will produce the following output −+------------+ | curdate()  | +------------+ | 2019-12-15 | +------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable1956    (    ProductId int,    ProductName varchar(20),    CustomerName varchar(20),    ShippingDate date    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1956 values(101, 'Product-1', 'Sam', '2019-10-11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1956 values(102, 'Product-2', 'Carol', '2018-12-01'); Query OK, 1 row affected (0.00 sec) ... Read More

MySQL - How can I fix an auto increment field with deleted rows from 1,2,3,4,5 to 1,3,5). Now we want it to be 1,2,3

AmitDiwan
Updated on 31-Dec-2019 07:01:43

494 Views

Let us first create a table −mysql> create table DemoTable1955    (    UserId int NOT NULL AUTO_INCREMENT    ,    PRIMARY KEY(UserId)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1955 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1955 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1955 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1955 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1955 values(); Query OK, 1 row affected (0.00 sec)Display ... Read More

Get day name for the corresponding date in MySQL?

AmitDiwan
Updated on 31-Dec-2019 06:59:51

79 Views

To fetch day name, use DAYNAME() function in MySQL. Let us first create a table −mysql> create table DemoTable1954    (    ShippingDate date    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1954 values('2019-12-15'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1954 values('2018-04-11'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1954 values('2019-01-31'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1954 values('2016-10-01'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * ... Read More

Display custom text in a new column on the basis of null values in MySQL?

AmitDiwan
Updated on 31-Dec-2019 06:57:30

475 Views

Let us first create a table −mysql> create table DemoTable1953    (    StudentName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1953 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1953 values(NULL); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1953 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1953 values(NULL); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1953;This will produce the following output −+-------------+ | ... Read More

Set custom messages on the basis of a column with student marks in MySQL

AmitDiwan
Updated on 31-Dec-2019 06:53:59

88 Views

For this, use CASE statement. Let us first create a table −mysql> create table DemoTable1952    (    Marks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1952 values(35); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1952 values(65); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1952 values(55); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1952 values(39); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1952;This will produce ... Read More

Split float value in two columns of a MySQL table?

AmitDiwan
Updated on 31-Dec-2019 06:50:55

617 Views

To split float value in two columns, the first column will have a value before decimal. The second column will have a value after decimal. For this, you can use SUBSTRING_INDEX() along with CAST(). Let us first create a table −mysql> create table DemoTable1951    (    Value1 varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1951 values('100.50'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1951 values('70.90'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1951 values('1000.55'); Query OK, 1 row affected ... Read More

Combine SUM and FORMAT in MySQL to format the result

AmitDiwan
Updated on 31-Dec-2019 06:48:29

2K+ Views

Let us create a table −mysql> create table DemoTable1950    (    Amount float    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1950 values(45.60); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1950 values(101.78); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1950 values(75.90); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1950 values(89.45); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1950;This will produce the following output −+--------+ | Amount ... Read More

Advertisements