Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mysql Substr index and multiple group by conditions

I’m trying to get the totals of a table grouped by names that are similar. Here is my test mysql:

    CREATE TABLE IF NOT EXISTS aa (
     clicks INT NOT NULL, 
     locn varchar (30) NOT NULL
    ) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
          
    insert into aa values(1, 'page?1'),   
                         (1, 'page?2'),
                         (1, 'page?3'),
                         (1, 'item(12)'),                     
                         (1, 'item(22)'),                     
                         (1, 'item(32)');                     

If I run this command

    SELECT count(clicks), substring_index(locn, '(',1) as a FROM aa group by a;

the result is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    3 item
    1 page?1
    1 page?2
    1 page?3

If I run

    SELECT count(clicks), substring_index(locn, '?',1) as b FROM aa group by b ;

the result is

    1 item(12)
    1 item(22)
    1 item(32)
    3 page

So each of those work but I can’t figure out how to get them to work together. Would someone explain how to do this, please?

>Solution :

SELECT count(clicks), substring_index(substring_index(locn, '(', 1), '?', 1) as a 
FROM aa group by a;

Result given your data:

+---------------+------+
| count(clicks) | a    |
+---------------+------+
|             3 | page |
|             3 | item |
+---------------+------+
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading