Found 4219 Articles for MySQLi

How do you append a carriage return to a value in MySQL?

Arjun Thakur
Updated on 30-Jun-2020 12:30:09

932 Views

You need to use CONCAT_WS() function from MySQL to append a carriage return. If you are looking for a new line, then append in the beginning. The syntax is as follows −SELECT CONCAT_WS(‘’, yourColumnName) as anyVariableName from yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table NewLineDemo -> ( -> CountryName varchar(10) -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into NewLineDemo values('US'); Query OK, 1 row affected (0.15 sec) ... Read More

How to get the sum for every distinct value in another column in MySQL?

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

2K+ Views

You can get the sum for every distinct value in another column with the help of aggregate function SUM() with GROUP BY command. To understand the above concept, let us create a table. The query to create a table is as follows:mysql> create table SumOfEveryDistinct -> ( -> Id int not null, -> Amount int -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into SumOfEveryDistinct values(10, 100); Query OK, 1 row affected (0.19 ... Read More

MySQL stored procedure return value?

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

12K+ Views

To return a value from stored procedure, you need to use user defined session specific variable. Add @ symbol before variable name.For example, use @symbol for variable valido. The syntax for the same is as follows:SELECT @valido;Whenever you use select statement, you need to use @anyVariableName. The syntax is as follows:SELECT @anyVariableName;Here is the demo of stored procedure of return value. The query to create a stored procedure is as follows:mysql> create procedure ReturnValueFrom_StoredProcedure -> ( -> In num1 int, -> In num2 int, -> out valido int ... Read More

Use a trigger to stop an insert or update in MySQL?

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

4K+ Views

You need to use SIGNAL SQL STATE command to stop an insert or update in MySQL. The trigger syntax is as follows:DELIMITER // CREATE TRIGGER yourTriggerName BEFORE INSERT ON yourTableName FOR EACH ROW BEGIN yourCondition THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'anyMessageToEndUser'; END // DELIMITER ;Now, create a trigger that would prevent to insert a record in the table on some condition. The query to create a trigger is as follows:mysql> DELIMITER // mysql> CREATE TRIGGER InsertPreventTrigger BEFORE INSERT ON Insert_Prevent    -> FOR EACH ROW    -> BEGIN    -> IF(new.Id < 1 or new.Id > 5) THEN ... Read More

Retrieve a large select by chunks in MySQL?

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

4K+ Views

To retrieve large select by chunks, you need to use ORDER BY LIMIT. The syntax is as follows:SELECT *FROM yourTableName ORDER BY yourColumnName LIMIT 0, 10;From the above syntax, you will get 10 rows from the table. In the above syntax, 0 represents the first row from the result set of a table that means it is zero index based. The second value of LIMIT represents the maximum number of rows that can be retrieved from the table.If you want the next rows after 10 to 30, then use in LIMIT like this. The syntax is as follows:SELECT *FROM yourTableName ... Read More

Split a string and loop through values in MySQL Procedure?

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

4K+ Views

To split a string and loop through all values in MySQL procedure, you do not need to use REPLACE() function. To understand, first create a stored procedure and after that create a table and call the stored procedure with some values. The value will be inserted into the table.The query to create a stored procedure is as follows:mysql> DELIMITER // mysql> CREATE PROCEDURE SP_SplitString(Value longtext)    -> BEGIN    -> DECLARE front TEXT DEFAULT NULL;    -> DECLARE frontlen INT DEFAULT NULL;    -> DECLARE TempValue TEXT DEFAULT NULL;    -> iterator:    -> LOOP      -> IF LENGTH(TRIM(Value)) ... Read More

Conditional NOT NULL case MySQL?

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

595 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

876 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

267 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

Advertisements