Found 4219 Articles for MySQLi

MySQL select query to fetch data with null value?

AmitDiwan
Updated on 30-Sep-2019 06:57:06

398 Views

Let us first create a table −mysql> create table DemoTable (    CustomerName varchar(100),    CustomerCountryName varchar(100) ); Query OK, 0 rows affected (0.95 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', 'US'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Bob', 'UK'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Carol', NULL); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David', 'AUS'); Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values('Mike', NULL); Query OK, 1 row affected (0.18 sec)Display all records ... Read More

Show column value twice in MySQL Select?

AmitDiwan
Updated on 30-Sep-2019 06:54:42

213 Views

You can use concat(). Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(100); Query ... Read More

Get count of zeros for columns values declared with INT type in MySQL

AmitDiwan
Updated on 30-Sep-2019 06:52:26

184 Views

For this, you can use LENGTH() along with REPLACE(). Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (1.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10002000); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(00000); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(400560); Query OK, 1 row affected (0.19 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----------+ | Value    | +----------+ | 10002000 ... Read More

Return value from a row if it is NOT NULL, else return the other row value in another column with MySQL

AmitDiwan
Updated on 30-Sep-2019 06:50:15

199 Views

For this, you can use IFNULL(). Let us first create a table −mysql> create table DemoTable (    FirstName varchar(100),    LastName varchar(100) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John', 'Doe'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values(NULL, 'Taylor'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('David', NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(NULL, 'Miller'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL query to compare and display only the rows with NULL values

AmitDiwan
Updated on 30-Sep-2019 06:48:14

112 Views

For this, you can use IFNULL(). Let us first create a table −mysql> create table DemoTable (    Value1 int,    Value2 int ); Query OK, 0 rows affected (0.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, NULL); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20, 40); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(3, NULL); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------+--------+ | Value1 | Value2 ... Read More

MySQL UNIQUE declaration to avoid inserting duplicate values?

AmitDiwan
Updated on 30-Sep-2019 06:46:13

194 Views

Following is the declaration for a UNIQUE clause in MySQL −create table yourTableName (    yourColumnName1 dataType,    yourColumnName2 dataType,    UNIQUE(yourColumnName1),    UNIQUE(yourColumnName1) );Let us first create a table −mysql> create table DemoTable (    Value int,    Value2 int,    UNIQUE(Value),    UNIQUE(Value2) ); Query OK, 0 rows affected (13.98 sec)Insert some records in the table using insert command. Here, duplicate records won’t insert because we have used UNIQUE above −mysql> insert into DemoTable values(10, 20) ; Query OK, 1 row affected (0.32 sec) mysql> insert into DemoTable values(10, 30); ERROR 1062 (23000): Duplicate entry '10' for key ... Read More

Display distinct dates in MySQL from a column with date records

AmitDiwan
Updated on 30-Sep-2019 06:44:28

416 Views

Let us first create a table −mysql> create table DemoTable (    AdmissionDate datetime ); Query OK, 0 rows affected (8.99 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2010-01-10 12:30:45'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2010-01-10 12:30:45'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('2010-01-10 12:30:45'); Query OK, 1 row affected (0.71 sec) mysql> insert into DemoTable values('2010-01-12 12:30:45'); Query OK, 1 row affected (1.22 sec) mysql> insert into DemoTable values('2010-01-20 12:30:45'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2010-01-20 ... Read More

Get all the tables from a MySQL database having a specific column, let’s say xyz?

AmitDiwan
Updated on 27-Sep-2019 08:00:45

158 Views

Let’s say we have a database “web” and we need to get all the tables having a specific column ’StudentFirstName’.For this, below is the query −mysql> select myColumnName.table_name from information_schema.columns myColumnName where myColumnName.column_name = 'StudentFirstName' and table_schema='web';This will produce the following output −+---------------+ | TABLE_NAME | +---------------+ | demotable109 | | demotable297 | | demotable335 | | demotable395 | | demotable418 | | demotable425 | | demotable436 | +---------------+ 7 rows in set (0.14 sec)Therefore, the above tables have one of the column names as “StudentFirstName”.Let us check the description ... Read More

Convert MySQL timestamp to UNIX Timestamp?

AmitDiwan
Updated on 27-Sep-2019 07:58:58

1K+ Views

To convert MySQL timestamp to UNIX Timestamp, use the UNIX_TIMESTAMP(). Following is the syntax −select unix_timestamp(yourColumnName) from yourTableName;Let us first create a table −mysql> create table DemoTable(    Duetimestamp timestamp ); Query OK, 0 rows affected (2.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(now()); Query OK, 1 row affected (1.53 sec) mysql> insert into DemoTable values('2016-01-21 12:34:00'); Query OK, 1 row affected (0.73 sec) mysql> insert into DemoTable values('2018-05-01 02:00:00'); Query OK, 1 row affected (0.42 sec) mysql> insert into DemoTable values('2017-03-02 01:10:20'); Query OK, 1 row affected (10.19 sec)Display all records from ... Read More

How can I replace & with an ampersand in my MySQL database?

AmitDiwan
Updated on 27-Sep-2019 07:57:41

594 Views

To replace & with an ampersand, use MySQL REPLACE(). Let us first create a table −mysql> create table DemoTable(    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value varchar(100) ); Query OK, 0 rows affected (1.06 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Value) values('@amp'); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable(Value) values('&'); Query OK, 1 row affected (1.09 sec) mysql> insert into DemoTable(Value) values('#amp'); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+----+-------+ ... Read More

Advertisements