这个MySQL查询返回3行,但我想只选择total_votes = max(total_votes);
的那一行,请告诉我该怎么办?
SELECT
assembly,
seat_code,
CONCAT(first_name, ' ', last_name),
total_votes,
party_id
FROM (polling JOIN seat USING (seat_id) JOIN candidate USING (candidate_id))
WHERE seat_id = 1;
答案 0 :(得分:2)
从
替换您的查询SELECT
assembly,
seat_code,
CONCAT(first_name,' ',last_name),
total_votes,
party_id
FROM (polling JOIN seat USING (seat_id) JOIN candidate USING (candidate_id))
WHERE seat_id=1;
到
SELECT
assembly,
seat_code,
CONCAT(first_name,' ',last_name),
total_votes,
party_id
FROM (polling JOIN seat USING (seat_id) JOIN candidate USING (candidate_id))
WHERE seat_id=1 AND total_votes=(SELECT MAX(total_votes) FROM <table name>);
答案 1 :(得分:0)
您可以使用MAX
聚合:
SELECT
assembly,
seat_code,
CONCAT(first_name,' ',last_name)name,
MAX(total_votes)total_votes,
party_id
FROM polling JOIN seat USING (seat_id) JOIN candidate USING (candidate_id)
WHERE seat_id=1;
注意:无需在JOIN部分放置括号。