MySQL COMPRESS() function
COMPRESS() function
MySQL COMPRESS() function compress a string and returns the result as a binary string.
Nonempty strings are stored as a four-byte length of the uncompressed string (low byte first), followed by the compressed string. If the string ends with space, an extra "." character is added to avoid problems with end space trimming.
Empty strings are stored as empty strings.
Syntax:
COMPRESS(string_to_compress);
Argument:
Name | Description |
---|---|
string_to_compress | A string which is to be compressed. |
Syntax Diagram:

MySQL Version: 5.6
Example
Code:
SELECT COMPRESS('text'),LENGTH(COMPRESS('text'));
Explanation:
The above MySQL statement will compress the string text and also returns the length of the string after compression.
Sample Output:
mysql> SELECT COMPRESS('text'),LENGTH(COMPRESS('text')); +------------------+--------------------------+ | COMPRESS('text') | LENGTH(COMPRESS('text')) | +------------------+--------------------------+ | xœ+I( gÆ | 16 | +------------------+--------------------------+ 1 row in set (0.03 sec)
PHP script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>example-compress - php mysql examples | w3resource</title>
</head>
<body>
<?php
echo "<h2>Compressing the text 'text' and returning the
result as a binary string : </h2>";
echo "<table border='1' style='border-collapse: collapse;
border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='100' align='center'>COMPRESS('text')</td><td width='100' align='center'>COMPRESS('text'), LENGTH(COMPRESS('text'))<
/td>";
echo "</tr>";
include("../dbopen.php");
$result = mysql_query("SELECT COMPRESS('text'),
LENGTH(COMPRESS('text')) as Length "); //Length is an alias
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center' width='200'>" .
$row["COMPRESS('text')"] . "</td>";
echo "<td align='center' width='200'>" .
$row["Length"] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
Previous: AES_ENCRYPT()
Next: DECODE()
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook