当我在联接查询中放置DISTINCT关键字时出现语法错误

时间:2018-08-18 11:25:27

标签: php codeigniter

DISTINCT关键字加入联接查询时出现语法错误

我通过联接查询为用户id使用DISTINCT

SELECT  `dev_test_team`. *, 
        DISTINCT `dev_user`.`user_id` as athlete_id, 
        `dev_user`.`user_type` 
FROM `dev_test_team`,`dev_user` 
JOIN `dev_test_teams_athlete` as `tt` ON  `dev_user`.`user_id` = `tt`.`user_id`
  

SQL Syntex错误为    #1064-您的SQL语法有误;请检查与您的MariaDB服务器版本相对应的手册,以获取正确的语法,以在'DISTINCT dev_useruser_id附近作为运动者ID,dev_useruser_type来自`dev_te' 1

我总是使用此查询获取此值

SELECT `dev_test_team`.*,`dev_user`.`user_id` as `athlete_id`,
        `dev_user`.`user_type` 
FROM `dev_test_team`,`dev_user` 
JOIN `dev_test_teams_athlete` as `tt` ON `dev_user`.`user_id` = `tt`.`user_id`

team_id |team_name| user_id |athlete_id | user_type 
 1          team1     480       380         4
 2          team2     472       380         4
 1          team1     480       379         4
 2          team2     472       379         4
 3          team3     478       360         4
 4          team3     478       390         4       

我想要像这样的值:-

team_id |team_name| user_id |athlete_id | user_type 
 1          team1     480       380         4
 2          team2     472       379         4
 3          team3     478       360,369     4

1 个答案:

答案 0 :(得分:0)

期待您的预期结果,您不应使用DISTINCT子句,而

您正在寻找group_concat与众不同

SELECT dev_test_tean.id, dev_test_team.name, dev_test_team.user_id, group_concat( distinct `dev_user`.`user_id`) as `athlete_id`,
        `dev_user`.`user_type` 
FROM `dev_test_team`
CROSS JOIN `dev_user` 
INNER JOIN `dev_test_teams_athlete` as `tt` ON `dev_user`.`user_id` = `tt`.`user_id`
GROUP BY dev_test_tean.id, dev_test_team.name, dev_test_team.user_id, `dev_user`.`user_type`