1
+---------+-------------+--------------+
|table_uid|  group_uid  | product_uid  |
+---------+-------------+--------------+
|    8901 |        5206 |          184 |
|    8902 |        5206 |          523 |
|    9194 |        5485 |          184 |
|    9195 |        5485 |          523 |
|    7438 |        1885 |          184 |
|    7439 |        1885 |          184 |
+---------+-------------+--------------+

My goal here is to show any group_uids that contain the same exact set of product_uids. So group_uid 5206 and 5485 would end up displaying while 1885 would not since it does not have the same set of product_uids. I have to accomplish this through bash script as I do not have the ability to do this in MySQL 3.23.58 (yes, it's horribly old and I hate it but not my choice). I'm trying to store the table_uid, group_uid and product_uid in an array and then compare each group_uid to see if they contain the same product_uids. Help is appreciated!

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
Walker Boh
  • 750
  • 6
  • 13

2 Answers2

1

How are you displaying the table above?

You might have some success with GROUP_CONCAT Can I concatenate multiple MySQL rows into one field?

SELECT group_uid, GROUP_CONCAT(product_uid,separator ','), count(*)
FROM <tab>
GROUP BY group_uid
HAVING count(*) > 1

I'm not sure how it would order the strings as I don't have mysql at present

Community
  • 1
  • 1
KeepCalmAndCarryOn
  • 8,817
  • 2
  • 32
  • 47
  • Unfortunately, group_concat is not in mysql 3.23.58. I've thoroughly searched my options to get this done in mysql but I cannot. It would have to be done via bash script. But thanks for the suggestion – Walker Boh Aug 22 '13 at 11:11
1

Here's a bit of awk to collect the groups that belong to the same set of products:

awk '
    NR > 3 && NF == 7 {
        prod[$4] = prod[$4] $6 " "
    } 
    END {
        for (group in prod) 
            groups[prod[group]] = groups[prod[group]] group " "
        for (key in groups) 
            print key ":" groups[key]
    }
' mysql.out 
184 523 :5206 5485 
184 184 :1885 

If you know which set of product ids you're interested in, you can pass that in:

awk -v prod_ids="184 523" '
    NR > 3 && NF == 7 {
        prod[$4] = prod[$4] $6 " "
    } 
    END {
        for (group in prod) 
            groups[prod[group]] = groups[prod[group]] group " "
        key = prod_ids " "
        if (key in groups)
            print groups[key]
    }
' mysql.out 
5206 5485 
glenn jackman
  • 238,783
  • 38
  • 220
  • 352