Found 4381 Articles for MySQL

Conditional NOT NULL case MySQL?

George John
Updated on 30-Jul-2019 22:30:24

811 Views

For conditional NOT NULL case, you do not need to use and = operator. You need to use IS NULL and IS NOT NULL property because NULL is a special case in MySQL.To understand the conditional NOT NULL case, let us create a table. The query to create a table is as follows:mysql> create table ConditionalNotNullDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> SendMessage longtext, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table ... Read More

MySQL truncate text with ellipsis?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

1K+ Views

You can truncate the text with ellipsis using LENGTH() with CASE statement. If your length is greater than 7 then truncate the text and add some number otherwise print the number as it is.To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table TruncateText    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Number longtext,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.66 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into TruncateText(Number) values('64575868667687'); ... Read More

Does MySQL update with regexp possible?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

464 Views

You cannot update with regexp i.e. you need to use LIKE operator instead of regexp. MySQL does not provide support for update with regexp. The LIKE operator is as follows:UPDATE yourTableName SET yourColumnName= REPLACE(yourColumnName, yourValue)', '' ) WHERE yourColumnNameLIKE '%yourValueThatWillReplace)%';To understand the above syntax, let us create a table.mysql> create table Replace_Demo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Value varchar(20),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into Replace_Demo(Value) values('221)'); Query OK, 1 ... Read More

Changing Column in MySQL from int to double?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

4K+ Views

To change the column in MySQL from int to double you need to use ALTER TABLE command.The syntax is as follows:ALTER TABLE yourTableName modify column yourColumnName DOUBLE NOT NULL;If you want NULL value then remove NOT NULL from the above syntax. The syntax is as follows:ALTER TABLE yourTableName modify column yourColumnName DOUBLE;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table IntToDoubleDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(10),    -> CylinderVolume int,    -> PRIMARY KEY(Id)    -> ); Query OK, ... Read More

Can MySQL automatically convert empty strings to NULL?

George John
Updated on 30-Jul-2019 22:30:24

15K+ Views

You need to use NULLIF() function from MySQL. The syntax is as follows:SELECT NULLIF(yourCoumnName, ’ ’) as anyVariableName from yourTableName;In the above syntax, if you compare empty string( ‘ ‘) to empty string( ‘ ‘), the result will always be NULL. However, if you compare with NULL to empty string( ‘ ‘) then also the result will always be NULL.To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table ConvertEmptyStringToNULL    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> PRIMARY KEY(Id)   ... Read More

In MySQL how to select the top 2 rows for each group?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

1K+ Views

To select the top 2 rows from each group, use the where condition with subquery. Let us create a table. The query to create a table is as follows:mysql> create table selectTop2FromEachGroup    -> (    -> Name varchar(20),    -> TotalScores int    -> ); Query OK, 0 rows affected (0.80 sec)Now insert some records in the table using insert command. The query is as follows:mysql> insert into selectTop2FromEachGroup values('John', 32); Query OK, 1 row affected (0.38 sec) mysql> insert into selectTop2FromEachGroup values('John', 33); Query OK, 1 row affected (0.21 sec) mysql> insert into selectTop2FromEachGroup values('John', 34); Query OK, ... Read More

How can I add a new column which counts the number of rows as serial number in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

1K+ Views

To add a new column that counts the number of rows as serial number, you can use the global variable in select statement.Let us create a table. The query to create a table is as follows:mysql> create table addColumnToCountAsSerialNumber    -> (    -> Id int,    -> Name varchar(20),    -> Age int,    -> Salary int    -> ); Query OK, 0 rows affected (0.80 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into addColumnToCountAsSerialNumber values(10, 'John', 23, 8576); Query OK, 1 row affected (0.10 sec) mysql> insert into addColumnToCountAsSerialNumber values(12, ... Read More

The equivalent of SQL Server function SCOPE_IDENTITY() in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

2K+ Views

The equivalent of SQL Server function SCOPE_IDENTITY() is equal to LAST_INSERT_ID() in MySQL. The syntax is as follows:SELECT LAST_INSERT_ID().This returns the id of last inserted record.Here, I am going to create a table with primary key column. The following is the demo of last_insert_id().First, let us create two tables. The query to create the first table table is as follows:mysql> create table TestOnLastInsertIdDemo    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (0.95 sec)Now creating the second table. The query is as follows:mysql> create table TestOnLastInsertIdDemo2   ... Read More

How to match one character in MySQL in place of %?

George John
Updated on 30-Jul-2019 22:30:24

185 Views

To match only one character in MySQL, you can use underscore(_) in place of %. The syntax is as follows:SELECT *FROM yourTableName WHERE yourColumnName LIKE ‘yourString_’;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table OneCharactermatchDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> PassoutYear year,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into OneCharactermatchDemo(PassoutYear) values('2008'); Query OK, 1 row affected (0.14 sec) mysql> ... Read More

Convert output of MySQL query to UTF8?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

3K+ Views

You need to use CAST() or CONVERT() function to convert output of MySQL query to UTF8. Here, I am using MySQL version 8.0.12. Let us first check the version:mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.12 | +-----------+ 1 row in set (0.00 sec)In this if you use utf8 then you will get warning of aliases because it has utf8mb4. Therefore, you can avoid the warning by placing utf8mb4.Note: Never use UTF8. For current versions, use UTF8MB4Here is the syntax to convert output of MySQL query to UTF8:SELECT yourColumnName1, convert(yourColumnName2 USING utf8) as anyVariableName FROM ... Read More

Advertisements