Found 4219 Articles for MySQLi

Using UNIQUE for varchar columns with some conditions in MySQL?

AmitDiwan
Updated on 16-Dec-2019 07:28:14

177 Views

For this, you can use UNIQUE constraint on one or more columns −alter table yourTablleName add unique(yourColumnName1, yourColumnName2, ...N);Let us first create a table −mysql> create table DemoTable1598    -> (    -> EmployeeId int,    -> EmployeeName varchar(20),    -> EmployeeCountryName varchar(20)    -> ); Query OK, 0 rows affected (0.52 sec)Here is the query to implement UNIQUE on varchar columns −mysql> alter table DemoTable1598 add unique(EmployeeName, EmployeeCountryName); Query OK, 0 rows affected (0.55 sec) Records: 0  Duplicates: 0  Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable1598 values(101, 'Adam', 'AUS'); Query OK, 1 ... Read More

Fix ERROR 1093 (HY000): You can't specify target table for update in FROM clause while deleting the lowest value from a MySQL column?

AmitDiwan
Updated on 16-Dec-2019 07:20:03

992 Views

Let us first create a table −mysql> create table DemoTable1597    -> (    -> Marks int    -> ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1597 values(45); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1597 values(59); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1597 values(43); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1597 values(85); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1597 values(89); Query OK, 1 row affected (0.12 sec)Display all records from the table ... Read More

Can we use “rank” as column name with MySQL8?

AmitDiwan
Updated on 16-Dec-2019 07:18:54

3K+ Views

The rank is a MySQL reserved word defined in MySQL version 8.0.2. Therefore, you cannot use rank as a column name. You need to use backticks around the rank.Let us first check the MySQL version we are working on. Here, I am using MySQL version 8.0.12 −mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.12    | +-----------+ 1 row in set (0.00 sec)The issues by using “rank” as column name are as follows −mysql> create table DemoTable1596    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> rank int   ... Read More

MySQL RegExp to fetch records with only a specific number of words

AmitDiwan
Updated on 16-Dec-2019 07:17:13

139 Views

For this, use Regular Expression in MySQL as in the below syntax −select * from yourTableName where yourColumnName regexp '\land[\land ]+[ ]+[\land ]+$';The above query will work when the two words are separated by a space. Let us first create a table −mysql> create table DemoTable1412    -> (    -> Name varchar(40)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1412 values('John Adam Carol'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1412 values('Mike Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert ... Read More

Call Stored Procedures within a Stored Procedure with IF Logic?

AmitDiwan
Updated on 16-Dec-2019 07:16:09

481 Views

To call stored procedures within a stored procedure, the syntax is as follows −If yourInputValue > 100 then      call yourProcedureName1();  else     call yourProcedureName2();     end If ;     ENDLet us implement the above syntax. In order to implement the above concept, let us create a stored procedure −mysql> delimiter // mysql> create procedure Hello_Stored_Procedure()    -> BEGIN    -> select 'Hello World!!!';    -> END    -> // Query OK, 0 rows affected (0.18 sec)The query to create the second stored procedure is as follows −mysql> create procedure Hi_Stored_Procedure()    -> BEGIN    -> ... Read More

Use MySQL REGEXP to ignore number and get only String and '/'

AmitDiwan
Updated on 16-Dec-2019 07:13:57

332 Views

For this, use REGEXP_REPLACE(). Let us first create a table −mysql> create table DemoTable1595    -> (    -> StudentCode varchar(50)    -> ); Query OK, 0 rows affected (0.44 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1595 values('200 John'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1595 values('101 Carol/400 Taylor'); Query OK, 1 row affected (0.72 sec) mysql> insert into DemoTable1595 values('101 302 405 Sam/9870'); Query OK, 1 row affected (0.28 sec)Display all records from the table using select statement −mysql> select * from DemoTable1595;This will produce the following output −+----------------------+ ... Read More

Select nth highest value in MySQL

AmitDiwan
Updated on 16-Dec-2019 07:12:56

191 Views

To select the nth highest value in MySQL, following is the syntax −select distinct(yourColumnName) from yourTableName order by yourColumnName DESC limit (NthValue-1), 1;Let us first create a table −mysql> create table DemoTable1594    -> (    -> Marks int    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1594 values(76); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1594 values(95); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1594 values(56); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1594 values(96); Query OK, ... Read More

Alternative to MySQL CASE WHEN in MySQL

AmitDiwan
Updated on 16-Dec-2019 07:04:51

532 Views

Use IF() method as an alternative to CASE WHEN in MySQL. Let us first create a table −mysql> create table DemoTable1593    -> (    -> PlayerScore int    -> ); Query OK, 0 rows affected (0.44 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1593 values(78); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1593 values(0); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1593 values(89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1593 values(0); Query OK, 1 row affected (0.16 sec)Display all records from the table using ... Read More

Only update the MySQL field if the field contains null or 0?

AmitDiwan
Updated on 16-Dec-2019 06:52:29

137 Views

For this, set conditions using MySQL IF(). Let us first create a table −mysql> create table DemoTable1592    -> (    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1592 values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1592 values(NULL); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1592 values(98); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1592 values(0); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1592 values(75); Query OK, 1 row affected ... Read More

How to convert varchar “time” to real time in MySQL?

AmitDiwan
Updated on 16-Dec-2019 06:51:20

315 Views

For this, you can use TIME_FORMAT(). Let us first create a table −mysql> create table DemoTable1591    -> (    -> ArrivalTime varchar(20)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1591 values('1620'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable1591 values('2345'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1591 values('2210'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select * from DemoTable1591;This will produce the following output −+-------------+ | ArrivalTime | +-------------+ ... Read More

Advertisements