简体   繁体   中英

How to make MySQL DISTINCT work with multiple columns using table join?

How can I made the query below select only one record? Each game has two records (one record for each team). If there are nine (9) games there would be eighteen (18) records. I want to select only one record, not both records, for each game.

If I use DISTINCT with only one column (game_id_2) it works fine or returns only nine records. However, if I try to add more columns the DISTINCT directive no longer works.

SELECT DISTINCT
B.game_id_2,
B.GmeYear,
B.GmeMonth,
B.GmeDay,
B.GmeDate,
B.GmeTime,
B.GmeOrd,
B.Home,
B.DivPlay,
L.Instit,
FROM BsbGme B LEFT JOIN LeagueTeam L
ON B.team_id = L.team_id
WHERE B.NonD1=''
AND B.team_id IN ($participant_str)
AND B.GmeMonth = $GameMonth
AND B.GmeDay = $GameDay
ORDER BY B.game_id_2 ASC

According to w3schools dot come the DISTINCT directive is supposed to work with multiple columns. But it's not working with multiple columns in my example.

w3schools example:

SQL SELECT DISTINCT Syntax SELECT DISTINCT column_name,column_name FROM table_name ;

http://www.w3schools.com/sql/sql_distinct.asp

I have looked at several of the other answers to this same question on StackOverflow but I can't get any of them to work.

Thank you in advance.

尝试使用括号 DISTINCT(您的,列)或使用 GROUP BY。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM