mysql distinct的坑

1  Distinct 位置

  单独的distinct只能放在开头,否则报错,语法错误

mysql> Select  player_id,distinct(task_id) from task;
ERROR 1064 (42000): You havean error in your SQL syntax; check the manual that
corresponds to your MySQLserver version for the right syntax to use near 'disti
nct(task_id) from task' atline 1
现在把distinct放在开头

mysql> Select  distinct(task_id),taskid from task;
查询成功

2 需要返回mysql表中2列以上的结果时会有歧义
比如SELECT DISTINCT player_id, task_id FROM task;
实际上返回的是player_id与task_id同时不相同的结果。在这种情况下,distinct同时作用了两个字段,player_id,task_id。和我们的期望是很不一样的,好大的坑啊

猜你喜欢

转载自huangyunbin.iteye.com/blog/1913616
今日推荐