1

我写了这个查询来查找销售什么类别的商店列表。

SELECT GROUP_CONCAT(distinct(sub_category_id)) AS s,
             vendor_id AS v
      FROM link_products_lists_vendors 
      GROUP BY vendor_id;

这导致,

+---------------------------------------+------------+
| category_ids                          | vendor_ids |
+---------------------------------------+------------+
| 24,28,25,16,26,23,27,2                |          3 |
| 2                                     |         67 |
| 19,28,17,16,20,2                      |         68 |
| 19,28,24,26,23,21,16,27,22,17,25,2    |        122 |
| 16,2                                  |        123 |
| 28,17,22,21,18,16,26,27,20,23,25,2    |        124 |
| 22,19,21,20,16,24,28,25,23,26,2       |        125 |
| 23,24,26,25,28,16,20,27,19,2          |        126 |
| 19,26,28,18,20,27,22,16               |        127 |
| 22,26,28,21,23,20,24,19,16,17,27,25,2 |        128 |
| 2                                     |        129 |
| 2                                     |        133 |
| 19,20,28,16,27,25,21,23,26,24,22      |        135 |
| 23,28,17,22,26,21,16,20,27,24,25,2    |        136 |
| 19,17,16,21,23,26,22,25,27,20,28      |        137 |
| 19,20,26,22,21,24,23,17,28,16,27,25,2 |        138 |
| 19,20,23,28,26,21,24,16,27,22,25,17,2 |        139 |
| 22,27,20,21,24,17,23,28,26,19,25,2    |        142 |
| 19,28,17,20,2                         |        143 |
+---------------------------------------+------------+
19 rows in set (0.01 sec)

我现在想要的是这样的,

+--------------------------  -----------+--- ----------+
| category_names                        | vendor_names |
+---------------------------------------+--------------+
| mobiles,laptops,desktops              |    abcShop   |
| mobiles                               |    xyzShop   |
| desktops,mouses,keyboards             |    pqrShop   |
+---------------------------------------+--------------+

我有类别表,

+----+---------------+
| id | name          |
+----+---------------+
| 17 | desktops      |
| 18 | external_hdds |
| 26 | headphones    |
| 27 | headsets      |
| 22 | keyboards     |
| 16 | laptops       |
| 24 | memory_cards  |
|  2 | mobile-phones |
| 21 | mouses        |
| 25 | pendrives     |
| 19 | printers      |
| 20 | routers       |
| 23 | speakers      |
| 28 | tablets       |
+----+---------------+

供应商表为,

+-----+----------------------+
| id  | name                 |
+-----+----------------------+
| 108 | abcShop              |
| 109 | xyzShop              |
|  45 | pqrShop              |
|  89 | .                    |
|  63 | .                    |
|  64 | .                    |
+-----+----------------------+

我应该如何编写一个不显示 id 但使用显示 id 并显示名称的表的查询?我不知道从哪里开始。请帮忙!

4

1 回答 1

3

Just join in the other tables and select those values instead

SELECT GROUP_CONCAT(distinct(c.name)) AS s,
       v.name AS v
  FROM link_products_lists_vendors l
  JOIN categories c on l.category_id = c.id
  JOIN vendors v on l.vendor_id = v.id
 GROUP BY v.name;

This works as long as vendor names and description names are unique.

于 2013-07-05T11:03:56.450 回答