Rama Giri has Published 110 Articles

Sum the values in a column in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

249 Views

For this, use the aggregate function SUM(). Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> € int    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert ... Read More

Return the first n letters of a column in MySQL

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

71 Views

To return the first n letters, use the LEFT() function. Following is the syntax −select left(yourColumnName, yourValue) from yourTableName;Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CourseTitle text    -> ); Query OK, 0 ... Read More

GROUP BY a column in another MySQL table

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

210 Views

For this, you can use CREATE TABLE AS SELECT statement. Let us first create a table −mysql> create table DemoTable1    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> CountryName varchar(20)    -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the ... Read More

How can I set 0 if a query returns a null value in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

617 Views

For this, you can use IFNULL(). 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 (0.71 sec)Insert some records in the table using insert command ... Read More

How to sort multiple columns with a single query?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

184 Views

Let us first create a table −mysql> create table DemoTable     -> (     -> Id int,     -> Value int     -> ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 85885); Query ... Read More

MySQL query to select records beginning from a specific id

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(100)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('John'); Query ... Read More

MySQL query to select rows older than a week?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

216 Views

For this, you can use DATEDIFF() function. The current date time is as follows −mysql> select now(); +---------------------+ | now() | +---------------------+ | 2019-06-09 19:15:56 | +---------------------+ 1 row in set (0.00 sec)Let us first create ... Read More

How to set a comma separated list as a table in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

346 Views

You can use UNION ALL for this.Let us get list 10, 20, 30, 40, 50 as a table with UNION ALL −mysql> select 10 Number UNION ALL select 20 Number UNION ALL select 30 Number     UNION ALL select 40 Number UNION ALL select 50 Number;Output+--------+ | Number | +--------+ | 10 | | 20 | | 30 ... Read More

MySQL query to update string field by concatenating to it?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

410 Views

For concatenating a string field, use CONCAT() function. Let us first create a table −mysql> create table DemoTable    -> (    -> SequenceId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentId varchar(100)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using ... Read More

How to work with auto incrementing column in MySQL?

Rama Giri

Rama Giri

Updated on 30-Jul-2019 22:30:26

83 Views

To work with auto incrementing column, you can set it as AUTO_INCREMENT while creating the table.Let us first create a table. Here, we have set the Id field as column since that would be our auto increment column −mysql> create table DemoTable    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> FirstName varchar(20), ... Read More

Advertisements