2

I'm storing single emojis in a CHAR column in a MySQL database. The column's encoding is utf8mb4.

When I run this aggregate query, MySQL won't group by the emoji characters. It instead returns a single row with a single emoji and the count of all the rows in the database.

SELECT emoji, count(emoji) FROM emoji_counts GROUP BY emoji

Here's my table definition:

CREATE TABLE `emoji_counts` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `emoji` char(1) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Is there some special Unicode behavior I'll have to account for?

2
  • Where does the query run from? Do you know if you're encoding correctly on the way in? Or that the data in the table itself is good?
    – pvg
    Dec 12, 2015 at 21:28
  • Right now, running query straight through the console on Sequel Pro. Data in the table looks good -- the emojis display properly with a SELECT * FROM emoji_counts query
    – arm5077
    Dec 12, 2015 at 21:31

1 Answer 1

4

Turns out I needed to specify an expanded collation in the query, namely utf8mb4_unicode_520_ci.

This worked:

SELECT emoji, count(emoji) FROM emoji_counts group by emoji collate utf8mb4_unicode_520_ci;

EDIT: That collation isn't available on some server configs (including ClearDB's)... utf8mb4_bin also appears to work.

1
  • 2
    That is utterly terrifying.
    – pvg
    Dec 12, 2015 at 21:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.