Found 4378 Articles for MySQL

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

How to remove Duplicate Records except a single record in MySQL?

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

1K+ Views

You can use DELETE command with some condition for this since we need to keep one record and delete rest of the duplicate records.Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(40) ); Query OK, 0 rows affected (0.48 sec)Insert records in the table using insert command −mysql> insert into DemoTable(StudentName) values('John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(StudentName) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentName) values('Sam'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable(StudentName) ... Read More

How to select from a set of integers in MySQL?

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

180 Views

To select from a set of integers, you can use UNION. Following is the syntax −SELECT yourValue1 UNION SELECT yourValue2 UNION SELECT yourValue3 UNION SELECT yourValue4 . . . . NLet us implement the above syntax to select from a set of integers in MySQL −mysql> SELECT 1000 UNION SELECT 2000 UNION SELECT 3000 UNION SELECT 4000 UNION SELECT 5000 UNION SELECT 6000 UNION SELECT 7000;This will produce the following output −+------+ | 1000 | +------+ | 1000 | | 2000 | | 3000 | | 4000 | | 5000 | | 6000 | | 7000 | +------+ 7 rows in set (0.03 sec)

Replace dot with comma on MySQL SELECT?

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

5K+ Views

To replace dot with comma on SELECT, you can use REPLACE(). Following is the syntax −select replace(yourColumnName, '.' , ', ' ) from yourTableName;Let us first create a table −mysql> create table DemoTable (    Value float ); Query OK, 0 rows affected (0.63 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(4.56); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(456.23); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(1078.3); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(2000.50); Query OK, 1 row affected (0.26 sec) mysql> ... Read More

Which datatype is should I use to set a column for 5-star rating?

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

537 Views

You can use ENUM datatype for this. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserRating ENUM('1', '2', '3', '4', '5') ); Query OK, 0 rows affected (0.54 sec)Insert records in the table using insert command −mysql> insert into DemoTable(UserRating) values('5'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(UserRating) values('3'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(UserRating) values('1'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(UserRating) values('3'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable(UserRating) values('4'); ... Read More

MySQL query to select too many rows?

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

406 Views

You can use LIMIT for this, which is used to fetch limited number of records. Let us first create a table −mysql> create table DemoTable (    Id int,    Name varchar(20) ); Query OK, 0 rows affected (0.53 sec)Insert records in the table using insert command −mysql> insert into DemoTable values(10, 'John'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(11, 'Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(12, 'David'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(13, 'Carol'); Query OK, 1 row affected (0.13 sec) mysql> ... Read More

Advertisements