SET @test = 'test'; SELECT COERCIBILITY(@test), COERCIBILITY('test');
From the documentation :
COERCIBILITY(str)
The returned values have the values shown in the following table. Lower values have higher priority.
Coercibility Meaning Example 0 Explicit collation Value with COLLATE clause 1 No collation Concatenation of strings with different collations 2 Implicit collation Column value 3 System constant USER() return value 4 Coercible Literal string 5 Ignorable NULL or an expression derived from NULL
Coercivity determines what will be converted to that in the event of a sort conflict.
An expression with a higher coercivity will be converted to matching an expression with a lower coercivity.
This feature is useful for troubleshooting sorting problems. For example, these two queries return results in a different order.
This:
SELECT col FROM ( SELECT DATABASE() AS col UNION ALL SELECT 'X' ) q ORDER BY col;
And this one:
SET @t := 'X' COLLATE UTF8_BIN; SELECT col FROM ( SELECT DATABASE() AS col UNION ALL SELECT @t ) q ORDER BY col;
Why is that?
DATABASE() is a system function whose return values have a coercivity of 3 and the default database sort is UTF8_GENERAL_CI .
'X' in the first query is a string literal with a capacity of 4 .
The result of UNION will always have the least coercivity of all values (i.e. 3 ) and matching the expression with the least ability:
SELECT col, COERCIBILITY(col), COLLATION(col) FROM ( SELECT DATABASE() AS col UNION ALL SELECT 'X' ) q ORDER BY col;
In the second query, @t is a variable that contains a string value with UTF8_BIN sorting. Since its coercivity is lower than that of a system function, this is a sorting variable that is used in the result set.
The coercivity of variable 2 , so the coercivity of the result is a variable, as well as a comparison:
SET @t := 'X' COLLATE UTF8_BIN; SELECT col, COERCIBILITY(col), COLLATION(col) FROM ( SELECT DATABASE() AS col UNION ALL SELECT @t ) q ORDER BY col;