简体   繁体   中英

Case insensitive GROUP BY query in SQLite

I am trying to make query and make case insensitive grouping of results by a field. Here is what I tried, but it doesn't work.

Lower keyword after group by doesn't make any difference.

"SELECT " + MediaStore.Audio.Media.ARTIST + ", count(LOWER(" + MediaStore.Audio.Media.ARTIST +
                    ")) FROM " + PlaylistDB.TABLE_ALL_TRACKS + " GROUP BY LOWER(" + MediaStore.Audio.Media.ARTIST
                    +") HAVING " + MediaStore.Audio.Media.ARTIST
                    + " LIKE ? ORDER BY "+ MediaStore.Audio.Media.ARTIST + " COLLATE NOCASE ASC";

Also I tried COLLATE NOCASE. Also doesn't work.

"SELECT " + MediaStore.Audio.Media.ARTIST + ", count(LOWER(" + MediaStore.Audio.Media.ARTIST +
                ")) FROM " + PlaylistDB.TABLE_ALL_TRACKS + " GROUP BY LOWER(" + MediaStore.Audio.Media.ARTIST
                +") HAVING " + MediaStore.Audio.Media.ARTIST
                + " LIKE ? ORDER BY "+ MediaStore.Audio.Media.ARTIST + " COLLATE NOCASE ASC";

Is there anything you can suggest me to get case insensitive grouping?

I fix the issue. The lower keyword works with group by fine. But my mistake was that some fields were with additional space at the end. So I fixed it by using the trim keyword. The code below works as I need

"SELECT " + MediaStore.Audio.Media.ARTIST + ", count(LOWER(TRIM(" + MediaStore.Audio.Media.ARTIST +
                "))) FROM " + PlaylistDB.TABLE_ALL_TRACKS + " GROUP BY LOWER(TRIM(" + MediaStore.Audio.Media.ARTIST
                +")) HAVING " + MediaStore.Audio.Media.ARTIST
                + " LIKE ? ORDER BY "+ MediaStore.Audio.Media.ARTIST + " COLLATE NOCASE ASC";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM