Found 4219 Articles for MySQLi

How to prevent MySQL double insert (duplicate entry)?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

825 Views

To prevent duplicate entry, add constraint UNIQUE. Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(100) ); Query OK, 0 rows affected (0.79 sec)Here is the query to prevent MySQL double insert using UNIQUE −mysql> alter table DemoTable add constraint id_NameUnKey UNIQUE(Id, Name); Query OK, 0 rows affected (0.82 sec) Records: 0 Duplicates: 0 Warnings: 0Insert records in the table using insert command. When we will try to the same record again, then the “Duplicate entry” error will be visible −mysql> insert into DemoTable values(11, 'John'); Query OK, 1 row affected (0.18 ... Read More

How to align a column right-adjusted in MySQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can use LPAD() from MySQL for this. Let us first create a table −mysql> create table DemoTable (    FullName varchar(100) ); Query OK, 0 rows affected (0.81 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Sam Williams'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('Carol Taylor'); Query OK, 1 row affected (0.47 sec)Display records from the table using select command −mysql> select *from DemoTable;This ... Read More

Select all except the first character in a string in MySQL?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

852 Views

To select all except the first character in a string, you can use SUBSTR() method. Let us first create a table −mysql> create table DemoTable (    FirstName varchar(20) ); Query OK, 0 rows affected (0.63 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('Larry'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec)Display records from the table using select command −mysql> ... Read More

How to select data from a table where the table name has blank spaces in MYSQL?

Samual Sam
Updated on 30-Jul-2019 22:30:25

783 Views

You need to use backticks around the table name where the table name has blank space. Let us first create a table. Here, we have used backtick −mysql> create table `Demo Table138` (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Price int ); Query OK, 0 rows affected (0.47 sec)Insert records in the table using insert command −mysql> insert into `Demo Table138`(Price) values(450); Query OK, 1 row affected (0.18 sec) mysql> insert into `Demo Table138`(Price) values(499); Query OK, 1 row affected (0.16 sec) mysql> insert into `Demo Table138`(Price) values(199); Query OK, 1 row affected (0.17 sec) mysql> insert into ... Read More

Get the Average of Average in a single MySQL row?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

191 Views

You can use aggregate function AVG() for this. Let us first create a table −mysql> create table DemoTable (    Value1 int,    Value2 int,    Value3 int ); Query OK, 0 rows affected (0.54 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(10, 20, 30); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(13, 15, 18); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(21, 31, 41); Query OK, 1 row affected (0.21 sec)Display records from the table using select command −mysql> select *from DemoTable;This will produce the following ... Read More

Ordering alphabetically in MySQL except for one entry?

Samual Sam
Updated on 30-Jul-2019 22:30:25

113 Views

You can use ORDER BY clause for this. Let us first create a table −mysql> create table DemoTable (    FirstName varchar(200) ); Query OK, 0 rows affected (0.93 sec)Insert records in the table using insert command −mysql> insert into DemoTable values('Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Carol'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Sam'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('Mike'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.18 sec)Display records from the ... Read More

How to subtract by 1 if the field value > 0 in MySQL?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

271 Views

You can use CASE statement with UPDATE command for this. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Value int ); Query OK, 0 rows affected (1.44 sec)Insert records in the table using insert command −mysql> insert into DemoTable(Value) values(100); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable(Value) values(0); Query OK, 1 row affected (4.16 sec) mysql> insert into DemoTable(Value) values(104); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(Value) values(0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Value) values(5); Query ... Read More

What is the easiest way to store date in MySQL database?

Samual Sam
Updated on 30-Jul-2019 22:30:25

187 Views

To store date in MySQL, use the STR_TO_DATE() method −insert into yourTableName values(STR_TO_DATE('yourDate', '%d/%m/%Y'));Let us first create a table −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.62 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(STR_TO_DATE('10/01/2013', '%d/%m/%Y')); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(STR_TO_DATE('31/01/2015', '%d/%m/%Y')); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(STR_TO_DATE('23/04/2019', '%d/%m/%Y')); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(STR_TO_DATE('01/03/2019', '%d/%m/%Y')); Query OK, 1 row affected (0.48 sec)Display records from the table using select ... Read More

Set MySQL int column to auto increment by 1 beginning at 10000?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

437 Views

Let us first create a table. Here, we have set UserId as AUTO_INCREMENT PRIMARY KEY −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY ); Query OK, 0 rows affected (0.72 sec)Following is the query to set int column to auto increment by 1 beginning at 10000 −mysql> alter table DemoTable AUTO_INCREMENT=10000; Query OK, 0 rows affected (0.31 sec) Records: 0 Duplicates: 0 Warnings: 0Insert records in the table using insert command −mysql> insert into DemoTable values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.19 sec) ... Read More

MySQL order by from highest to lowest value?

Samual Sam
Updated on 30-Jul-2019 22:30:25

2K+ Views

To order by from highest to lowest value, you can use ORDER BY DESC command −select *from yourTableName order by yourColumnName DESC;If you want the result from lowest to highest, you can use ORDER BY ASC command −select *from yourTableName order by yourColumnName ASC;Let us first create a table −mysql> create table DemoTable (    Value int ); Query OK, 0 rows affected (0.56 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(134); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(245); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable ... Read More

Advertisements