Found 4219 Articles for MySQLi

How does COALESCE order results with NULL and NON-NULL values?

AmitDiwan
Updated on 04-Oct-2019 08:31:21

604 Views

The COALESCE() finds the NON-NULL value first If it finds the same in the beginning, then it returns, otherwise moves ahead to check NON-NULL value.Let us first create a table −mysql> create table DemoTable (    Number1 int,    Number2 int ); Query OK, 0 rows affected (5.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 200); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable values(NULL, 50); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10, NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable ... Read More

MySQL query to find latest 3 dates in a table and the resultant dates shouldn’t be duplicate

AmitDiwan
Updated on 04-Oct-2019 08:28:52

364 Views

To find the latest dates, order the date records with ORDER BY DESC. Since we want only 3 dates, use LIMIT 3.Let us first create a table −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-09-04'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('2019-08-10'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('2019-09-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-09-18'); Query OK, 1 row affected (0.17 sec) mysql> ... Read More

Display specific name from a table with repeated individual FirstName and LastName using LIKE clause twice

AmitDiwan
Updated on 04-Oct-2019 08:26:25

64 Views

Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(30) ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentName) values('John Smith'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentName) values('David Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName) values('Carol Taylor'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(StudentName) values('John Doe'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentName) values('Chris Brown'); Query OK, 1 row ... Read More

How to extract only the numbers from a text field in MySQL?

AmitDiwan
Updated on 04-Oct-2019 08:23:53

911 Views

Let us first create a table −mysql> create table DemoTable (    Number text ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('7364746464, -'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('-, 8909094556'); Query OK, 1 row affected (0.23 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------------+ | Number | +--------------+ | 7364746464, - | | -, 8909094556 | +--------------+ 2 rows in set (0.00 sec)Following is ... Read More

MySQL query to convert height format to centimeters?

AmitDiwan
Updated on 04-Oct-2019 08:15:01

444 Views

For this, use the CAST() method in MySQL. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentHeight varchar(40) ) ; Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentHeight) values('5\'10\"'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentHeight) values('4\'6\"'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentHeight) values('5\'8\"'); Query OK, 1 row affected (0.10 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output ... Read More

How to sum varchar column and display the count in MySQL?

AmitDiwan
Updated on 04-Oct-2019 08:09:55

590 Views

For this, use GROUP BY along with COUNT(*). Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeGender varchar(40) ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeGender) values('MALE'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(EmployeeGender) values('FEMALE'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(EmployeeGender) values('FEMALE'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(EmployeeGender) values('FEMALE'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(EmployeeGender) values('MALE'); Query ... Read More

Format date with DATE_FORMAT() and STR_TO_DATE() in MySQL

AmitDiwan
Updated on 04-Oct-2019 08:01:09

242 Views

Let us first create a table −mysql> create table DemoTable (    DueDate varchar(100) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('August 04, 2019'); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----------------+ | DueDate | +----------------+ | August 04, 2019 | +----------------+ 1 row in set (0.00 sec)Following is the query to format date −mysql> set @stringToDate=(select date_format(str_to_date(DueDate, '%M %d, %Y'), '%Y-%m-%d') from ... Read More

How can I set my auto-increment value to begin from 1 in MySQL?

AmitDiwan
Updated on 04-Oct-2019 07:58:49

351 Views

You can truncate the table to set auto_increment value to start from 1 in MySQL. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY ); Query OK, 0 rows affected (1.44 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.06 sec)Display all records ... Read More

To return value of a number raised to the power of another number, we should use ^ operator in MySQL?

AmitDiwan
Updated on 04-Oct-2019 07:53:54

61 Views

No, ^ is the Bitwise XOR operator in MySQL. For this, use POW() or POWER() from MySQL. Let us first create a table &minuns;mysql> create table DemoTable (    BaseValue int,    PowerValue float ); Query OK, 0 rows affected (0.48 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(4, 1.9867); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(10, 6.789); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(20, 8.9); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from ... Read More

How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?

AmitDiwan
Updated on 04-Oct-2019 07:51:33

298 Views

Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    CustomerName varchar(20),    ProductPrice int ); Query OK, 0 rows affected (0.70 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(CustomerName, ProductPrice) values('Chris', 600); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(CustomerName, ProductPrice) values('David', 450); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable(CustomerName, ProductPrice) values('Chris', 980); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable(CustomerName, ProductPrice) values('Mike', 1200); Query OK, 1 row affected (0.11 sec) mysql> insert into ... Read More

Advertisements