Found 4219 Articles for MySQLi

Implement and set DOUBLE length in MySQL

AmitDiwan
Updated on 24-Dec-2019 06:20:19

408 Views

To implement DOUBLE in MySQL, the syntax is as follows −create table yourTableName      (      yourColumnName double(5, 2) unsigned );Let us first create a table −mysql> create table DemoTable1814      (      Amount double(5, 2) unsigned      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1814 values(1.98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1814 values(100.24); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1814 values(198.50); Query OK, 1 row affected (0.00 sec)Display all records from the table ... Read More

How to GRANT SELECT ON all tables in all databases on a server with MySQL?

AmitDiwan
Updated on 24-Dec-2019 06:19:03

1K+ Views

For this, you can use GRANT SELECT statement as in the below syntax −GRANT SELECT ON *.* TO 'yourUserName'@'yourHostName';First list all the user names along with host −mysql> select user, host from mysql.user;This will produce the following output −+------------------+-----------+ | user             |      host | +------------------+-----------+ | Bob              |         % | | Charlie          |         % | | Robert           |         % | | User2       ... Read More

Implement If else in stored procedure in MySQL?

AmitDiwan
Updated on 24-Dec-2019 06:40:10

272 Views

To implement if-else, the syntax is as follows −if yourCondition then      yourStatement1;     else     yourStatement2;     end if ;To understand the above concept for if-else in a stored procedure, let us create a stored procedure −mysql> delimiter // mysql> create procedure If_else_stored_demo(value int)      begin      if value > 1000 then      select "your value is greater than 1000";      else      select "your value is less than or equal to 1000";      end if ;      end      // Query OK, 0 rows affected (0.00 ... Read More

How to select and display a list of values in one column that are available in two different MySQL columns?

AmitDiwan
Updated on 24-Dec-2019 06:15:52

2K+ Views

For this, use UNION ALL. Let us first create a table −mysql> create table DemoTable1813      (      Name1 varchar(20),      Name2 varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1813 values('John', 'Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1813 values('Adam', 'Robert'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1813 values('Mike', 'Sam'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1813;This will produce the following ... Read More

How to extract column name and type from MySQL?

AmitDiwan
Updated on 24-Dec-2019 06:14:15

409 Views

To extract column name and type, use INFORMATION_SCHEMA.COLUMNS −select concat(column_name, '=', data_type) as anyAliasName from information_schema.columns where table_schema= yourDatabaseName and table_name= yourTableName;Let us first create a table −mysql> create table DemoTable1812      (      Id int,      FirstName varchar(20),      Age int,      isMarried boolean,      status ENUM('ACTIVE', 'INACTIVE')      ); Query OK, 0 rows affected (0.00 sec)Here is the query to extract column name and type from MySQL:mysql> select concat(column_name, '=', data_type) as COLUMNNAMEANDTYPE from information_schema.columns      where table_schema= 'web' and table_name= 'DemoTable1812';This will produce the following output −+-------------------+ | COLUMNNAMEANDTYPE ... Read More

Adding unique constraint to ALTER TABLE in MySQL

AmitDiwan
Updated on 24-Dec-2019 06:13:14

307 Views

Let us first create a table −mysql> create table DemoTable1811      (      FirstName varchar(20),      LastName varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Here is the query to add indexmysql> alter table DemoTable1811 ADD UNIQUE unique_index_first_last_name(FirstName, LastName); Query OK, 0 rows affected (0.00 sec) Records: 0  Duplicates: 0  Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable1811 values('John', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1811 values('John', 'Doe'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1811 values('Adam', 'Smith'); Query OK, 1 ... Read More

How to make 'from' as column name in MySQL?

AmitDiwan
Updated on 24-Dec-2019 06:11:46

474 Views

Use the backticks symbol to consider ‘from’ as column name since it is a reserved word. We will now create a table with from reserved word surrounded by backtick −mysql> create table DemoTable1810      (      `from` varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1810 values('US'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1810 values('UK'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1810 values('AUS'); Query OK, 1 row affected (0.00 sec)Display all records from the table using ... Read More

Fetch a specific column value (name) in MySQL

AmitDiwan
Updated on 24-Dec-2019 06:10:32

671 Views

To fetch a specific column value, use LIKE clause. Let us first create a table −mysql> create table DemoTable1809      (      Name varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1809 values('John'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1809 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1809 values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1809 values('Johnson'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement ... Read More

UNIX_TIMESTAMP with date in MySQL query to fetch records after a specific date in different format?

AmitDiwan
Updated on 25-Feb-2020 13:12:18

218 Views

For this, you can use STR_TO_DATE(), since we have date records in the following format: 21/11/2019.Let us first create a table −mysql> create table DemoTable1808      (      AdmissionDate varchar(20)      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1808 values('21/11/2019'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1808 values('01/01/2018'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1808 values('26/09/2017'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1808;This will ... Read More

Display all the column values in a single row separated by comma in MySQL?

AmitDiwan
Updated on 24-Dec-2019 06:08:19

1K+ Views

For this, use GROUP_CONCAT() and CONCAT(). Let us first create a table −mysql> create table DemoTable1807      (      Id int      ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1807 values(101); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1807 values(102); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1807 values(103); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1807;This will produce the following output −+------+ | Id   | ... Read More

Advertisements