Found 4219 Articles for MySQLi

How to check if a date has passed in MySQL?

AmitDiwan
Updated on 05-Nov-2019 07:19:46

789 Views

Let us first create a table −mysql> create table DemoTable1340    -> (    -> Deadline date    -> ); Query OK, 0 rows affected (0.43 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1340 values('2019-09-18'); Query OK, 1 row affected (0.52 sec) mysql> insert into DemoTable1340 values('2019-09-23'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1340 values('2018-12-24'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1340 values('2016-11-01'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1340 values('2019-09-28'); Query OK, 1 row affected (0.18 sec)Display all records from the table ... Read More

MySQL random rows sorted by a specific column name?

AmitDiwan
Updated on 05-Nov-2019 07:17:29

218 Views

Let us first create a table −mysql> create table DemoTable1339    -> (    -> Name varchar(30),    -> Score int    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1339 values('Chris', 56); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable1339 values('Bob', 46); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1339 values('Adam', 78); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1339 values('John', 90); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1339 values('Carol', 98); Query OK, 1 ... Read More

Count values based on conditions and display the result in different columns with MySQL?

AmitDiwan
Updated on 05-Nov-2019 07:15:06

82 Views

Let us first create a table −mysql> create table DemoTable1485     -> (     -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,     -> StudentName varchar(20),     -> StudentSubject varchar(20)     -> ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1485(StudentName, StudentSubject) values('Chris', 'MySQL'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1485(StudentName, StudentSubject) values('Robert', 'MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1485(StudentName, StudentSubject) values('Robert', 'MongoDB'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1485(StudentName, StudentSubject) ... Read More

Using MySQL where clause and ordering by avg() to find the average of duplicate individual elements

AmitDiwan
Updated on 05-Nov-2019 07:12:03

190 Views

For this, use having clause instead of where. Let us first create a table −mysql> create table DemoTable1338    -> (    -> Name varchar(10),    -> Score int    -> ); Query OK, 0 rows affected (1.54 sec)Insert some records in the table using insert command. Here, we have inserted duplicate names with scores −mysql> insert into DemoTable1338 values('Chris', 8); Query OK, 1 row affected (0.80 sec) mysql> insert into DemoTable1338 values('Bob', 4); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1338 values('Bob', 9); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1338 values('Chris', 6); ... Read More

Fetch maximum value from a column with values as string numbers like Value440, Value345, etc. in SQL

AmitDiwan
Updated on 05-Nov-2019 07:08:47

39 Views

For this, you can use MAX() along with substring(). Let us first create a table −mysql> create table DemoTable1337    -> (    -> Value varchar(50)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1337 values('Value400'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1337 values('Value345'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1337 values('Value567'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1337 values('Value489'); Query OK, 1 row affected (0.22 sec)Display all records from the table using select statement ... Read More

Display all grants of a specific user in MySQL

AmitDiwan
Updated on 05-Nov-2019 07:05:58

122 Views

Let us first display all usernames along with host name −mysql> select user, host from MySQL.user;This will produce the following output −+------------------+-----------+ | user             | host      | +------------------+-----------+ | Bob              | %         | | Charlie          | %         | | Robert           | %         | | User2            | %         | | mysql.infoschema | %         ... Read More

Select different fields in MySQL even if a field is set to null?

AmitDiwan
Updated on 05-Nov-2019 07:01:32

123 Views

For this, you can use COALESCE(). Let us first create a table −mysql> create table DemoTable1336    -> (    -> FirstName varchar(20)    -> ,    -> SecondName varchar(20)    -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1336 values('John', NULL); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1336 values(NULL, 'Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1336 values('David', 'Mike'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select * from DemoTable1336;This ... Read More

Select entries with timestamp after X time in MySQL

AmitDiwan
Updated on 05-Nov-2019 06:59:01

337 Views

Let us first create a table −mysql> create table DemoTable1335    -> (    -> ArrivalTime datetime    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command. We have inserted date time records here −mysql> insert into DemoTable1335 values('2019-09-19 22:54:00'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable1335 values('2019-09-19 22:59:00'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1335 values('2019-09-19 22:56:00'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1335 values('2019-09-19 22:52:00'); Query OK, 1 row affected (0.11 sec)Display all records from the table using ... Read More

Insert values in two tables with a single stored procedure call in MySQL

AmitDiwan
Updated on 05-Nov-2019 06:56:13

1K+ Views

Following is the syntax to insert values in two tables with a stored procedure −DELIMITER // CREATE PROCEDURE yourProcedureName(anyVariableName int)    BEGIN    insert into yourTableName1(yourColumnName1) values(yourVariableName);    insert into yourTableName2(yourColumnName2) values(yourVariableName);    END //Let us first create a table −mysql> create table DemoTable1    -> (    -> StudentScore int    -> ); Query OK, 0 rows affected (0.58 sec)Following is the second table −mysql> create table DemoTable2    -> (    -> PlayerScore int    -> ); Query OK, 0 rows affected (0.52 sec)Here is the query to create a stored procedure and insert values in two tables ... Read More

MySQL row declarations for ZF?

AmitDiwan
Updated on 05-Nov-2019 06:53:33

155 Views

The ZF stands for ZEROFILL i.e. row declarations for zero fill Let us first create a table. Here, we have set the int field size to be 10 −mysql> create table DemoTable1332    -> (    -> Number int(10) ZEROFILL NOT NULL DEFAULT 0    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1332 values(); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1332 values(1); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1332 values(10); Query OK, 1 row affected (0.10 sec) mysql> insert ... Read More

Advertisements