开发者

Mysql Distinct function nested within other function in select

Is there a way we can use disctinct for below cd.date_id? It's getting concatenated but I would also like distinct function aswell so it's like GROUP_CONCAT( CAST( distinct(cd.date_id) AS CHAR ) ) As date_id but it does not seem to work...

SELECT b.bar_id, b.bar_name, b.bar_image, b.bar_lat, b.bar_lng, b.bar_address,  
       b.bar_phone, b.bus_web, b.bar_open_hours, c.coupon_id, c.coupon_text,  
       bc.coupon_start_time, bc.coupon_exp_time, 
       GROUP_CONCAT( CA开发者_如何学PythonST( cd.date_id AS CHAR ) ) As date_id,
       d.calender_date, bc.bc_id, bc.priority As priority 
       FROM bars b 
       JOIN bars_coupons bc ON (bc.bar_id = b.bar_id) 
       JOIN coupons c ON (bc.coupon_id = c.coupon_id) 
       JOIN calendardates cd ON (cd.bc_id = bc.bc_id)
       JOIN date d ON (d.date_id = cd.date_id)
       GROUP BY cd.bc_id


DISTINCT inside there would not make any sense. What do you want to do here exactly? It seems you want only one row per distinct cd.date_id.

If that's the case just add cd.date_id to your GROUP BY clause at the end. Like this:

GROUP BY cd.bc_id, cd.date_id

And as you are using MySQL, you need not worry about using aggregate functions on all selected columns, if they would have the same value in all rows Grouped by the Group By clause, if not MySQL will just pick the first one

UPDATE
Looking at documentation for GROUP_CONCAT, you might be able to do it like this

GROUP_CONCAT( DISTINCT CAST(cd.date_id AS CHAR) ) As date_id
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜