Found 6702 Articles for Database

MySQL query to append a number of stars based on string length?

AmitDiwan
Updated on 08-Nov-2019 11:17:51

157 Views

For this, you can use RPAD(). Let us first create a table −mysql> create table DemoTable1626     -> (     -> Name varchar(20)     -> ); Query OK, 0 rows affected (0.37 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1626 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1626 values('Bob'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable1626 values('Robert'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select * from DemoTable1626; This will produce the following output −+--------+ ... Read More

Adding dash between spaces in field name in MySQL?

AmitDiwan
Updated on 08-Nov-2019 11:16:51

349 Views

You can use REPLACE() for this. Let us first create a table −mysql> create table DemoTable1625     -> (     -> FullName varchar(20)     -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1625 values('John Doe'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1625 values('Adam Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1625 values('John Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1625 values('Carol Taylor'); Query OK, 1 row affected (0.14 sec)Display all records from the ... Read More

SELECT * WHERE var == [one of many alternatives] in MySQL?

AmitDiwan
Updated on 08-Nov-2019 11:15:50

178 Views

Use IN() for select * where var== [one of many alternatives]. Let us first create a table −mysql> create table DemoTable1624     -> (     -> ClientId int,     -> ClientName varchar(20)     -> ); Query OK, 0 rows affected (0.39 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1624 values(101, 'Chris Brown'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1624 values(102, 'David Miller'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1624 values(103, 'John Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1624 ... Read More

How to sort an alphanumeric column with different lengths in MySQL?

AmitDiwan
Updated on 08-Nov-2019 11:14:54

121 Views

Let us first create a table −mysql> create table DemoTable1623     -> (     -> StudentCode varchar(20)     -> ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1623 values('STU-MIT-143'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1623 values('STU-MIT-10'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1623 values('STU-MIT-150'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1623 values('STU-MIT-148'); Query OK, 1 row affected (0.22 sec)Display all records from the table using select statement −mysql> select * from DemoTable1623; This ... Read More

How to search for the exact string value in MySQL?

AmitDiwan
Updated on 08-Nov-2019 11:10:21

131 Views

To search for the exact string value, use the concept of COLLATE. Let us first create a table −mysql> create table DemoTable1620     -> (     -> Subject varchar(20)     -> ); Query OK, 0 rows affected (0.42 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1620 values('mysql'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1620 values('MySql'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1620 values('mYSQL'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1620 values('MySQL'); Query OK, 1 row affected (0.26 sec) mysql> insert ... Read More

Convert datetime to get month name in MySQL?

AmitDiwan
Updated on 08-Nov-2019 11:09:24

276 Views

To get only month name, the syntax is as follows −select date_format(yourColumnName, '%M %Y') from yourTableName;Let us first create a table −mysql> create table DemoTable1619     -> (     -> ArrivalTime datetime     -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1619 values(now()); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable1619 values(curdate()); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1619 values('2019-12-31'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select * ... Read More

How can I enhance my select query to make it faster in MySQL?

AmitDiwan
Updated on 08-Nov-2019 11:08:21

99 Views

For quicker querying, use MySQL IN() because it uses indexing internally. Let us first create a table −mysql> create table DemoTable1618     -> (     -> ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,     -> ClientName varchar(20),     -> ClientEmailId varchar(30)     -> ); Query OK, 0 rows affected (1.53 sec)Insert some records in the table using insert command:mysql> insert into DemoTable1618(ClientName, ClientEmailId) values('Chris Brown', 'Brown323@gmail.com'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1618(ClientName, ClientEmailId) values('David Miller', 'MillerDavid@gmail.com'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1618(ClientName, ClientEmailId) values('John Doe', ... Read More

Create a Stored Procedure with MySQL and set a limit to display only a specific number of records

AmitDiwan
Updated on 08-Nov-2019 11:06:45

600 Views

Let us first create a table −mysql> create table DemoTable1368     -> (     -> ClientId int,     -> ClientName varchar(20)     -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1368 values(101, 'Adam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1368 values(102, 'Bob'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1368 values(103, 'John'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1368 values(104, 'Sam'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1368 values(105, ... Read More

How do I SELECT none of the rows and columns in MySQL?

AmitDiwan
Updated on 08-Nov-2019 11:05:25

567 Views

To display none of the rows and columns, use SELECT NULL and FALSE as in the below syntax −select null from yourTableName where false;Let us first create a table −mysql> create table DemoTable1367     -> (     -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,     -> FirstName varchar(20)     -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1367(FirstName) values('Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1367(FirstName) values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1367(FirstName) values('Bob'); ... Read More

Find specific records which has whitespace on the second place in MySQL

AmitDiwan
Updated on 08-Nov-2019 10:56:30

545 Views

For this, use SUBSTR() as in the below syntax −select * from yourTableName where substr(yourColumnName, 2, 1 ) = ' ';Let us first create a table −mysql> create table DemoTable1365     -> (     -> Value varchar(20)     -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1365 values('9756757474'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1365 values('3 45322333'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1365 values('8974646363'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1365 values('9 ... Read More

Advertisements