Found 4378 Articles for MySQL

MySQL query to fetch record by year

Sharon Christine
Updated on 30-Jun-2020 13:34:59

334 Views

To fetch record by year, use the YEAR() method in MySQL −select *from yourTableName where year(yourColumnName)=yourYearValue;Let us first create a table −mysql> create table DemoTable -> ( -> CustomerName varchar(100), -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', '2019-01-21'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('Robert', '2018-02-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David', '2016-04-01'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> ... Read More

MySQL query to create table dynamically?

Sharon Christine
Updated on 30-Jun-2020 13:36:25

2K+ Views

For this, you can use stored procedure. Let us create a table dynamically with two columns i.e. StudentId as int, whereas StudentName as varchar −mysql> DELIMITER $$ mysql> CREATE PROCEDURE creatingDynamicTableDemo(yourTableName VARCHAR(200))    -> BEGIN    ->    SET @name = yourTableName;    ->    SET @st = CONCAT('    '>       CREATE TABLE IF NOT EXISTS `' , @name, '` (    '>       `StudentId` int UNSIGNED NOT NULL AUTO_INCREMENT,    '>       `StudentName` varchar(20) NOT NULL,    '>    PRIMARY KEY (`StudentId`)    '>    )    '> ');    -> PREPARE ... Read More

Searching for an integer value in a varchar field in MySQL?

Sharon Christine
Updated on 30-Jun-2020 13:37:17

176 Views

To search for an integer value in a varchar filed, you can use CASE statement.Let us first create a table. Consider, we have a list of email-ids −mysql> create table DemoTable -> ( -> Title varchar(100) -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('9John@example.com'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('3Carol@gmail.com'); Query OK, 1 row affected (0.45 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-------------------+ | Title ... Read More

MySQL query to get the count of all the elements in the field?

karthikeya Boyini
Updated on 30-Jun-2020 13:40:15

71 Views

For this, use the COUNT() method. Let us first create a table −mysql> create table DemoTable -> ( -> ProductName varchar(100) -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Product-1'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('Product-2'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Product-3'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Product-3'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('Product-2'); Query OK, 1 row affected ... Read More

MySQL query to select distinct order by id

karthikeya Boyini
Updated on 30-Jun-2020 13:41:18

845 Views

For this, use ORDER BY MAX(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(10, 'John'); Query OK, 1 row affected (0.51 sec) mysql> insert into DemoTable values(10, 'John'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20, 'Sam'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(20, 'Sam'); Query OK, 1 row affected (0.11 sec) ... Read More

Comparing two columns in a single MySQL query to get one row?

Sharon Christine
Updated on 30-Jun-2020 13:42:26

572 Views

For this, you can use ORDER BY clause. Let us first create a table −mysql> create table DemoTable -> ( -> Num1 int, -> Num2 int -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(60, 249); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(59, 250); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+------+------+ | Num1 | Num2 | +------+------+ | 60 | 249 ... Read More

Discard last 3 characters of a field in MySQL

Sharon Christine
Updated on 30-Jun-2020 13:43:52

340 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('STU-090'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('STU-123'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('STU-678'); Query OK, 1 row affected (0.29 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will produce the following output −+-----------+ | StudentId | +-----------+ | STU-090 | ... Read More

Get the substring of a column in MySQL

karthikeya Boyini
Updated on 30-Jun-2020 13:44:57

1K+ Views

Use the SUBSTR() method to get the substring of a column.Let us first create a table −mysql> create table DemoTable -> ( -> Title text -> ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('This is a MySQL Database'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Java is an Object Oriented Programming Language'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select statement −mysql> select *from DemoTable ;OutputThis will produce the following output −+-------------------------------------------------+ | Title ... Read More

MySQL query to select date from timestamp?

karthikeya Boyini
Updated on 30-Jun-2020 13:21:07

373 Views

Let us first create a table. One of the column is a timestamp −mysql> create table DemoTable    -> (    -> CustomerName varchar(100),    -> CustomerShippingDate timestamp    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris', '2019-01-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David', '2019-03-01'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Robert', '2019-06-04'); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> select *from DemoTable;OutputThis will ... Read More

How do I select 5 random rows from the 20 most recent rows in MySQL?

Sharon Christine
Updated on 30-Jun-2020 13:22:49

395 Views

For random, use RAND() method. And for limit on rows, use the LIMIT() method.Let us first create a table −mysql> create table DemoTable    -> (    -> ShippingDate datetime    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('2019-01-03'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('2019-01-05'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2019-01-07'); Query OK, 1 row affected (0.11 sec) ... Read More

Advertisements