AmitDiwan has Published 11365 Articles

Display NULL and NOT NULL records except a single specific value in MySQL

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 13:04:54

126 Views

To display NULL records, use IS NULL in MySQL. To ignore a single value, use the != operator , which is an alias of the operator.Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    PlayerName varchar(40) ); Query ... Read More

MySQL throws an error when the table name “match” is not surrounded by single quotes?

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 13:02:44

150 Views

Do not use single quotes. You need to use backticks around the table name match, since it is a reserved name in MySQL. Following is the error that occurs :mysql> select *from match; ERROR 1064 (42000) : You have an error in your SQL syntax; check the manual that corresponds ... Read More

Fix MySQL ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 12:56:00

10K+ Views

To fix this error, you need to add PRIMARY KEY to auto_increment field. Let us now see how this error occurs −Here, we are creating a table and it gives the same error −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT,    StudentName varchar(40),    StudentAge int ... Read More

Get Nth weekday of the month from a column with DATE records in MySQL

AmitDiwan

AmitDiwan

Updated on 07-Oct-2019 12:51:19

197 Views

We need to find the weekday i.e. week 1 from date 1 to 7, week 2 from date 8 to 14, etc. To get the day, use DAY() function in MySQL. Set the conditions to get the weekday (number) using CASE statement.Let us now see an example and create a ... Read More

How can I select every alternative row and display in descending order in SQL?

AmitDiwan

AmitDiwan

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

3K+ 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,   ... Read More

Select all email addresses beginning with 5 numeric characters (regular expression) in MySQL

AmitDiwan

AmitDiwan

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

158 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 ... Read More

Get specific value of cell in MySQL

AmitDiwan

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 ... Read More

Using the value of an alias inside the same MySQL SELECT statement

AmitDiwan

AmitDiwan

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

628 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 (   ... Read More

Add some value to an int type column value in a table without knowing its current value in SQL?

AmitDiwan

AmitDiwan

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

105 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 ... Read More

Change date format in MySQL database table to d/m/y

AmitDiwan

AmitDiwan

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

302 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 ... Read More

Advertisements