有如下表A: 我想根据字段Material和model、name同时分组,条件:group by substring(Material,1,length(Material)-2),model,name 想要的结果如下,SQL如何写呢?请帮忙,感谢。 |
|
就是先通过字段Material(剔除最后2位)分组,然后再根据name和model来分组。
求大牛帮忙。谢谢。 |
|
70分 |
mysql> select * from ta; +-------+------------+-------------------------+------------------+ | name | product_id | model | Material | +-------+------------+-------------------------+------------------+ | TYLER | 1 | Armband for phone | A-AB00P133A-FT01 | | TYLER | 2 | Armband for phone | A-AB00P133A-FT02 | | TYLER | 3 | Armband for phone | A-AB00P133A-FT03 | | TYLER | 7 | Armband for phone | A-AB00P146A-FT01 | | TYLER | 8 | Armband for phone | A-AB00P146A-FT02 | | TYLER | 9 | Armband for phone | A-AB00P146A-FT03 | | TYLER | 49 | Armband for phone | A-AB00P162A-FT01 | | TYLER | 50 | Armband for phone | A-AB00P162A-FT02 | | HIPPO | 85 | Power for Home & Office | A-AD00-0002-EU | | HIPPO | 86 | Power for Home & Office | A-AD00-0002-ES | | KUGA | 88 | Power for Home & Office | A-AD00-0202-EU | | WOLF | 91 | Power for Home & Office | A-AD00-0402-BS | +-------+------------+-------------------------+------------------+ 12 rows in set (0.00 sec) mysql> select * -> from ta -> group by name,model,left(Material,length(Material)-2) -> order by product_id; +-------+------------+-------------------------+------------------+ | name | product_id | model | Material | +-------+------------+-------------------------+------------------+ | TYLER | 1 | Armband for phone | A-AB00P133A-FT01 | | TYLER | 7 | Armband for phone | A-AB00P146A-FT01 | | TYLER | 49 | Armband for phone | A-AB00P162A-FT01 | | HIPPO | 85 | Power for Home & Office | A-AD00-0002-EU | | KUGA | 88 | Power for Home & Office | A-AD00-0202-EU | | WOLF | 91 | Power for Home & Office | A-AD00-0402-BS | +-------+------------+-------------------------+------------------+ 6 rows in set (0.00 sec) mysql> |
30分 |
mysql> SELECT * FROM TTLA; +-------+------------+-------------------------+------------------+ | Name | product_id | model | Material | +-------+------------+-------------------------+------------------+ | TYLER | 1 | Armband for phone | A-AB00P133A-FT01 | | TYLER | 2 | Armband for phone | A-AB00P133A-FT02 | | TYLER | 3 | Armband for phone | A-AB00P133A-FT03 | | TYLER | 7 | Armband for phone | A-AB00P146A-FT01 | | TYLER | 8 | Armband for phone | A-AB00P146A-FT02 | | TYLER | 9 | Armband for phone | A-AB00P146A-FT03 | | TYLER | 49 | Armband for phone | A-AB00P162A-FT01 | | TYLER | 50 | Armband for phone | A-AB00P162A-FT02 | | HIPPO | 85 | Power for Home & Office | A-AD00-0002-EU | | HIPPO | 86 | Power for Home & Office | A-AD00-0002-ES | | KUGA | 88 | Power for Home & Office | A-AD00-0202-EU | | WOLF | 91 | Power for Home & Office | A-AD00-0402-BS | +-------+------------+-------------------------+------------------+ 12 rows in set (0.00 sec) mysql> SELECT * FROM TTLA A WHERE NOT EXISTS( -> SELECT 1 FROM TTLA WHERE A.`Name`=`Name` -> AND A.`model`=`model` -> AND SUBSTRING(A.Material,1,LENGTH(A.Material)-2)= -> SUBSTRING(Material,1,LENGTH(Material)-2) -> AND A.`product_id`>`product_id` -> ); +-------+------------+-------------------------+------------------+ | Name | product_id | model | Material | +-------+------------+-------------------------+------------------+ | TYLER | 1 | Armband for phone | A-AB00P133A-FT01 | | TYLER | 7 | Armband for phone | A-AB00P146A-FT01 | | TYLER | 49 | Armband for phone | A-AB00P162A-FT01 | | HIPPO | 85 | Power for Home & Office | A-AD00-0002-EU | | KUGA | 88 | Power for Home & Office | A-AD00-0202-EU | | WOLF | 91 | Power for Home & Office | A-AD00-0402-BS | +-------+------------+-------------------------+------------------+ 6 rows in set (0.00 sec) mysql> |
谢谢,为啥我的那种写法不行呢?
|
|
谢谢版主、
我再琢磨下。 |