Select Every Alternative Row and Display in Descending Order in SQL

AmitDiwan
Updated on 07-Oct-2019 12:45:42

4K+ Views

To fetch every alternative row, use MOD() under WHERE. Then use ORDER BY DESC to display the result in descending order −select *from yourTableName where mod(yourColumnName, 2)=1 order by yourColumnName DESC;Let us first create a table −mysql> create table DemoTable (    UniqueId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(40),    ClientAge int ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ClientName, ClientAge) values('Chris', 34); Query OK, 1 row affected (0.47 sec) mysql> insert into DemoTable(ClientName, ClientAge) values('Tom', 45); Query OK, 1 row affected (0.19 sec) mysql> ... Read More

Select All Email Addresses Beginning with 5 Numeric Characters in MySQL

AmitDiwan
Updated on 07-Oct-2019 12:41:17

232 Views

To get the email addresses beginning with 5 numeric characters, the optional solution is to use REGEXP −select *from yourTableName where yourColumnName regexp "^[0-9]{5}";Let us first create a table −mysql> create table DemoTable (    UserEmailAddress varchar(100) ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('6574John@gmail.com'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Carol23456@gmail.com'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('98989Chris_45678@gmail.com'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Mike12@gmail.com'); Query OK, 1 row affected (0.43 ... Read More

Get Specific Value of Cell in MySQL

AmitDiwan
Updated on 07-Oct-2019 12:39:36

1K+ Views

Let us first create a table −mysql> create table DemoTable (    Name varchar(40),    Score int ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris Brown', 78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('John Doe', 88); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Carol Taylor', 98); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David Miller', 80); Query OK, 1 row affected (0.68 sec)Display all records from the table using select statement −mysql> select *from ... Read More

Using Alias Value Inside MySQL SELECT Statement

AmitDiwan
Updated on 07-Oct-2019 12:37:47

824 Views

You cannot directly use an alias in the SELECT. Instead, use a user-defined variable. Following is the syntax. Here, @yourAliasName is our variable and alias −select @yourAliasName :=curdate() as anyAliasName, concat(‘yourValue.', yourColumnName, ' yourValue', @yourAliasName) as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    Name varchar(40) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David Miller'); ... Read More

Add Value to Int Column in SQL Without Knowing Current Value

AmitDiwan
Updated on 07-Oct-2019 12:35:18

187 Views

For this, simply use UPDATE command along with SET. Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentScore int ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(StudentScore) values(78); Query OK, 1 row affected (0.46 sec) mysql> insert into DemoTable(StudentScore) values(89); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(StudentScore) values(67); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(StudentScore) values(95); Query OK, 1 row affected (0.25 sec)Display all records from the table ... Read More

Change Date Format in MySQL Database Table to D-M-Y

AmitDiwan
Updated on 07-Oct-2019 12:31:43

381 Views

Following is the syntax −select date_format(yourColumnName, '%d/%m/%Y') as anyAliasName from yourTableName;Let us first create a table −mysql> create table DemoTable (    AdmissionDate date ); Query OK, 0 rows affected (0.89 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('2019-09-09'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2017-11-01'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This ... Read More

MySQL Query to Check if Multiple Rows Exist

AmitDiwan
Updated on 07-Oct-2019 12:29:39

780 Views

Let us first create a table −mysql> create table DemoTable1219 (    Id int,    Name varchar(40) ); Query OK, 0 rows affected (0.43 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1219 values(100, 'Adam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1219 values(101, 'John'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable1219 values(102, 'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1219 values(103, 'Bob'); Query OK, 1 row affected (0.18 sec)Display all records from the table using select statement −mysql> select *from DemoTable1219;This will produce the ... Read More

Increment Multiple Timestamp Values in SQL

AmitDiwan
Updated on 07-Oct-2019 12:20:14

287 Views

The incremented value can be set in a user-defined variable as shown below. Here, “yourValue” is the incremented value. After that, use MySQL UPDATE to update the column and increment timestamp values −set @anyVariableName :=yourValue; update yourTableName set yourColumnName=yourColumnName+interval (@yourVariableName) second;Let us first create a table −mysql> create table DemoTable (    DueDatetime timestamp ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-31 12 :30 :40'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values('2019-09-06 10 :00 :00'); Query OK, 1 row affected (0.73 sec) ... Read More

Counting Different Distinct Items in a Single MySQL Query

AmitDiwan
Updated on 07-Oct-2019 12:15:48

169 Views

To count items, use COUNT() along with DISTINCT. Here, DISTINCT is used to return distinct values. Let us now see an example and create a table −mysql> create table DemoTable (    CustomerId int,    CustomerName varchar(20),    ProductName varchar(40) ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(101, 'Chris', 'Product-1'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(102, 'David', 'Product-2'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(101, 'Chris', 'Product-1'); Query OK, 1 row affected (0.30 sec) mysql> insert ... Read More

Implement Case Sensitivity in MySQL SELECT Statements

AmitDiwan
Updated on 07-Oct-2019 12:13:09

278 Views

SELECT is by default case-insensitive. For case-sensitive implementation, the BINARY operator is used. Following is the syntax :select *from yourTableName where BINARY yourColumnName=yourValue;Let us first create a table −mysql> create table DemoTable (    Name varchar(40) ); Query OK, 0 rows affected (0.74 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('CHRIS'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('chris'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('CHris'); Query OK, 1 row affected (0.11 sec)Display ... Read More

Advertisements