Found 4381 Articles for MySQL

How can I implement an interval condition for due dates correctly in MySQL?

AmitDiwan
Updated on 02-Jul-2020 06:50:50

126 Views

Let us first create a table −mysql> create table DemoTable (DueDate date); Query OK, 0 rows affected (0.92 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-12'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-04-01'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-07-19'); Query OK, 1 row affected (0.15 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+------------+ | DueDate    | +------------+ | 2019-01-12 | | 2019-04-01 | | 2019-07-19 | +------------+ 3 rows in set (0.00 sec)Following is the query to ... Read More

Implementing incremental search and display the values with a specific number in MySQL?

AmitDiwan
Updated on 02-Jul-2020 06:51:42

168 Views

For this, you can use SUBSTRING_INDEX(). Let us first create a table −mysql> create table DemoTable (Number varchar(100)); Query OK, 0 rows affected (0.60 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('235678'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('1634990'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('678590'); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable values('908765432'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('222343388773'); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values('09028215'); Query OK, ... Read More

Select query to display duplicate values with max date

AmitDiwan
Updated on 02-Jul-2020 06:53:00

1K+ Views

For this, use GROUP BY and HAVING. Let us first create a table −mysql> create table DemoTable    (    StudentName varchar(100),    DueDate date    ); Query OK, 0 rows affected (0.72 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('John', '2019-01-11'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Chris', '2019-02-11'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Chris', '2019-03-11'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('John', '2019-04-11'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Bob', '2019-05-11'); ... Read More

Is there any alternative for CONCAT() in MySQL?

AmitDiwan
Updated on 02-Jul-2020 06:55:49

959 Views

Yes, an alternative is CONCAT_WS(). Let us first create a table −mysql> create table DemoTable    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(100)    ); Query OK, 0 rows affected (0.74 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable(StudentName) values('Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(StudentName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName) values('Bob'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | ... Read More

How to add a specific character to any empty space in MySQL table values?

AmitDiwan
Updated on 09-Mar-2020 08:32:46

392 Views

For this, use REPLACE() function and replace empty space with the character. Let us first create a table −mysql> create table DemoTable (Subject text); Query OK, 0 rows affected (0.86 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Java in depth'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('Data Structure and Algorithm'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+------------------------------+ | Subject | +------------------------------+ | Introduction ... Read More

MySQL query to select top n rows efficiently?

AmitDiwan
Updated on 02-Jul-2020 06:31:24

423 Views

Use index to select top n rows efficiently. Let us first create a table −mysql> create table DemoTable (StudentName varchar(100), StudentScore int ); Query OK, 0 rows affected (0.66 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('John', 34); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Carol', 55); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Bob', 58); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Sam', 38); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Mike', 48); Query OK, 1 row ... Read More

Select three random records with a fixed number of characters for each column value in MySQL

AmitDiwan
Updated on 02-Jul-2020 06:43:49

112 Views

For this, you can use CHAR_LENGTH(). Use RAND() for random records. Let us first create a table −mysql> create table DemoTable (Subject text); Query OK, 0 rows affected (0.61 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values('C'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('MySQL'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Java'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('MongoDB'); Query OK, 1 row affected (0.59 sec) mysql> insert into DemoTable values('RubyOnRails'); Query OK, 1 row affected (0.25 sec) mysql> insert ... Read More

Add a column to a MySQL table which is the result of concatenation of text and value from another auto increment column?

AmitDiwan
Updated on 20-Aug-2019 09:09:39

434 Views

For this, you can use LAST_INSERT_ID(). Let us first create a table −mysql> create table DemoTable    (    UserId int(6) unsigned zerofill NOT NULL AUTO_INCREMENT,    UserAutoIncrement char(100) default null,    PRIMARY KEY(UserId)    ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+--------+-------------------+ | UserId | UserAutoIncrement | +--------+-------------------+ | 000001 | NULL | +--------+-------------------+ ... Read More

Round seconds to nearest half minute in MySQL?

AmitDiwan
Updated on 02-Jul-2020 06:45:14

342 Views

To round seconds to nearest half minute, use CEILING(). Let us first create a table −mysql> create table DemoTable (secondValue int); Query OK, 0 rows affected (0.64 sec)ExampleInsert some records in the table using insert command −mysql> insert into DemoTable values(27); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(118); Query OK, 1 row affected (0.20 sec)Display all records from the table using select statement −mysql> select *from DemoTable;Output+-------------+ | secondValue | +-------------+ | 27          | | 56       ... Read More

How can I get the number of times a specific word appears in a column with MySQL?

Sharon Christine
Updated on 30-Jun-2020 14:59:08

1K+ Views

For this, you can use COUNT() function. Let us first create a table −mysql> create table DemoTable    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(100)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName) values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(EmployeeName) values('Carol'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(EmployeeName) values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(EmployeeName) values('Carol'); Query OK, 1 row affected ... Read More

Advertisements