Found 4219 Articles for MySQLi

How to SELECT fields from one table and INSERT to another in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

151 Views

Let us first create a table −mysql> create table DemoTable1    -> (    -> StudentId int,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1 values(10, 'John'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1 values(11, 'Chris'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable1;Output+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 10 | John ... Read More

MySQL query to get only the minutes from datetime?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

207 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> ShippingDate datetime    -> ); Query OK,  0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-10 10:04:45'); Query OK,  1 row affected (0.18 sec) mysql> insert into DemoTable values('2019-06-11 05:45:00'); Query OK,  1 row affected (0.23 sec) mysql> insert into DemoTable values('2019-06-12 07:00:55'); Query OK,  1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+---------------------+ | ShippingDate | +---------------------+ | 2019-01-10 10:04:45 | | 2019-06-11 05:45:00 | | 2019-06-12 07:00:55 | +---------------------+ 3 rows in set (0.00 sec)Here is the query to get minutes in MySQL −mysql> select minute(ShippingDate) as Minutes from DemoTable;Output+---------+ | Minutes | +---------+ | 4 ... Read More

MySQL query to select a count on two separate conditions?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

184 Views

Use CASE statement for this. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentMarks int,    -> isValid tinyint(1)    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(45, 0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(78, 1); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values(45, 1); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values(78, 1); Query OK, 1 row affected (0.22 sec) ... Read More

How to return rows that have the same column values in MySQL?

Naveen Singh
Updated on 30-Jul-2019 22:30:26

193 Views

Use GROUP BY clause for this. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int,    -> StudentMarks int    -> ); Query OK, 0 rows affected (4.71 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(23, 58); Query OK, 1 row affected (0.70 sec) mysql> insert into DemoTable values(25, 89); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values(26, 58); Query OK, 1 row affected (1.13 sec) mysql> insert into DemoTable values(28, 98); Query OK, 1 row affected (0.86 sec)Display ... Read More

Implement MySQL LIMIT and OFFSET in a single query stating its difference

Naveen Singh
Updated on 30-Jun-2020 09:56:32

615 Views

The LIMIT tells about how many records you want while OFFSET gives the records from the given position+1. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100)    -> ); Query OK, 0 rows affected (1.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 ... Read More

How to use a single MySQL query to count column values ignoring null?

Naveen Singh
Updated on 30-Jun-2020 09:57:40

113 Views

For this, you can COUNT() method, which does not include NULL value. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(100),    -> CountryName varchar(100)    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', null); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris', 'US'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Robert', null); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Bob', 'UK'); Query ... Read More

How to sort an alphanumeric column in MySQL?

Naveen Singh
Updated on 30-Jun-2020 09:58:33

560 Views

To sort an alphanumeric column, use LIKE operator along with SUBSTRING(). Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (1.54 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('S/TU/100'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('S/TU/1000'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('S/TU/10'); Query OK, 1 row affected (0.47 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following ... Read More

How to filter a specific month in MySQL when date is in varchar?

Naveen Singh
Updated on 30-Jun-2020 09:59:50

343 Views

To filter, you can use STR_TO_DATE() function from MySQL. With that, use MONTH() to get the date from the specific month. Let us first create a table −mysql> create table DemoTable    -> (   -> DueDate varchar(100)    -> ); Query OK, 0 rows affected (1.18 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('06-19-2019'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('01-31-2018'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values('12-01-2016'); Query OK, 1 row affected (0.14 sec)Display all records from the table using ... Read More

MySQL query to convert timediff() to seconds?

Naveen Singh
Updated on 30-Jun-2020 10:01:16

440 Views

For this, you can use TIME_TO_SEC() function. Let us first create a table −mysql> create table DemoTable    -> (    -> SourceTime time,    -> DestinationTime time    -> ); Query OK, 0 rows affected (1.33 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10:20:00', '4:50:54'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('12:05:10', '7:45:12'); Query OK, 1 row affected (0.30 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------------+-----------------+ | SourceTime | DestinationTime | +------------+-----------------+ | 10:20:00   ... Read More

MySQL query to get the count of distinct records in a column

Naveen Singh
Updated on 30-Jun-2020 10:04:28

224 Views

To get the count of distinct records, use DISTINCT along with COUNT(). Following is the syntax −select count(DISTINCT yourColumnName) from yourTableName;Let us first create a table −mysql> create table DemoTable -> ( -> Name varchar(20), -> Score int -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 56); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Sam', 89); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('John', 56); Query OK, 1 row affected (0.17 sec) mysql> insert into ... Read More

Advertisements