Mysql我组合了表格。但我不能做过滤排序

时间:2018-03-24 18:19:00

标签: php mysql

我将表格与group_concat合并。但我不能做过滤排序。我该怎么做?按ads_filter_option.filter_option ascdesc无法正常排序。

SQL-FIDDLE-TABLE

select
ads.id,
ads.ad_title,
GROUP_CONCAT(DISTINCT ads_filter.filter_title SEPARATOR ','),
GROUP_CONCAT(DISTINCT ads_filter_option.filter_option SEPARATOR ',')
from ads
LEFT JOIN ads_filter_option
on find_in_set(ads_filter_option.id,ads.filter_option_id)
LEFT JOIN ads_filter
on ads_filter.id=ads_filter_option.filter_id
where cat_1 = 2
group by ads.id

2 个答案:

答案 0 :(得分:1)

这似乎有效。这个结果与你想要的结果有什么不同?

select
ads.id,
ads.ad_title,
GROUP_CONCAT(DISTINCT ads_filter.filter_title SEPARATOR ','),
GROUP_CONCAT(DISTINCT ads_filter_option.filter_option SEPARATOR ',')
from ads
LEFT JOIN ads_filter_option
on find_in_set(ads_filter_option.id,ads.filter_option_id)
LEFT JOIN ads_filter
on ads_filter.id=ads_filter_option.filter_id
where cat_1 = 2
group by ads.id
order by GROUP_CONCAT(DISTINCT ads_filter_option.filter_option SEPARATOR ',')

如果您还想按选项类型订购,请尝试以下方法:

select
ads.id,
ads.ad_title,
GROUP_CONCAT(DISTINCT ads_filter.filter_title SEPARATOR ','),
GROUP_CONCAT(DISTINCT ads_filter_option.filter_option SEPARATOR ',')
from ads
LEFT JOIN ads_filter_option
on find_in_set(ads_filter_option.id,ads.filter_option_id)
LEFT JOIN ads_filter
on ads_filter.id=ads_filter_option.filter_id
where cat_1 = 2
group by ads.id
order by ads_filter.filter_title,GROUP_CONCAT(DISTINCT ads_filter_option.filter_option SEPARATOR ',')

以下是第二个查询的结果:

| id |                                                    ad_title | GROUP_CONCAT(DISTINCT ads_filter.filter_title SEPARATOR ',') | GROUP_CONCAT(DISTINCT ads_filter_option.filter_option SEPARATOR ',') |
|----|-------------------------------------------------------------|--------------------------------------------------------------|----------------------------------------------------------------------|
| 64 |                   Kiralık Araçlar deneme reyonsiverek ilanı |                                                        Color |                                                                Black |
| 59 |                          Otomobil deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 61 |              Arazi, SUV & Pick-up deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 63 |                    Ticari Araçlar deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 65 |                   Hasarlı Araçlar deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 66 |                    Deniz Araçları deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 69 |                  Modifiye Araçlar deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 71 |                               ATV deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 72 |                               UTV deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 74 |           Engelli Plakalı Araçlar deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,1.6 Engine |
| 62 |           Minivan, Van & Panelvan deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,2.0 Engine |
| 67 |                    Klasik Araçlar deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,2.0 Engine |
| 73 |                           Karavan deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     Black,2.0 Engine |
| 70 |                     Hava Araçları deneme reyonsiverek ilanı |                                                        Color |                                                                White |
| 47 | 1 AMG KIRMIZI HATASIZ SERVİS BAKIMLI CLA 200 İLK SAHİBİNDEN |                                                 Color,Engine |                                                     White,2.0 Engine |
| 60 |                        Motosiklet deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     White,2.0 Engine |
| 68 |                Elektrikli Araçlar deneme reyonsiverek ilanı |                                                 Color,Engine |                                                     White,2.0 Engine |

答案 1 :(得分:0)

使用group concat列的别名然后按该列别名使用顺序,试试这个

pattern