Evan Longoria homered early and hit a go-ahead, two-run single in the seventh inning as the San Francisco Giants rallied late again to beat the Oakland Athletics 5-4 on Sunday for a two-game sweep.

Donovan Solano added four hits and an RBI for the Giants in Bruce Bochy's 4,000th game as a major league manager. Buster Posey struck out three times and grounded into a double play.

More Images
San Francisco Giants manager Bruce Bochy walks toward the dugout after making a pitching change against the Oakland Athletics during the fifth inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Evan Longoria homered early and hit a go-ahead, two-run single in the seventh inning as the San Francisco Giants rallied late again to beat the Oakland Athletics 5-4 on Sunday for a two-game sweep.

San Francisco Giants' Kevin Pillar, right, slides into home to score a run next to Oakland Athletics catcher Chris Herrmann during the second inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

The A's remain a half-game behind Tampa Bay for the second AL wild card.

San Francisco Giants' Evan Longoria, right, is congratulated by Austin Slater after hitting a solo home run against the Oakland Athletics during the third inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Sam Coonrod (4-0) retired three batters to win. Will Smith pitched the ninth for his 30th save.

Oakland Athletics' Mark Canha, right, is congratulated by third base coach Matt Williams after hitting a solo home run against the San Francisco Giants during the fourth inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

The Giants won three of four against their Bay Area rivals this season.

San Francisco Giants shortstop Brandon Crawford fields a groundout by Oakland Athletics' Robbie Grossman during the first inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

CANHA'S DAY

Former Oakland Athletics player Rickey Henderson, left, gestures next to former teammate Dave Stewart before a baseball game between the Athletics and the San Francisco Giants in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Stephen Vogt, who hit a three-run homer as part of the Giants' eight-run eighth inning Saturday night, narrowly missed going deep again twice during the same at-bat in the sixth. Vogt hit a deep fly that curled just outside the left field foul pole, then came up short when Chad Pinder made a leaping grab at the fence.

Former Oakland Athletics player Dave Stewart throws out the ceremonial first pitch before a baseball game between the Athletics and the San Francisco Giants in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

HONORING '89 CHAMPS

Mark Canha homered twice for Oakland, both off starter Logan Webb. Canha also struck out with the bases loaded to end the fifth.

San Francisco Giants manager Bruce Bochy walks toward the dugout after making a pitching change against the Oakland Athletics during the fifth inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

San Francisco Giants manager Bruce Bochy walks toward the dugout after making a pitching change against the Oakland Athletics during the fifth inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

The A's remain a half-game behind Tampa Bay for the second AL wild card.

Less than 24 hours after scoring eight times in the eighth inning of a 10-5 win, the Giants fell behind early and trailed 4-3 before loading the bases against Jake Diekman (1-7) with none out in the seventh.

Blake Treinen replaced Diekman and struck out Posey to end an 11-pitch battle. Longoria followed with a first-pitch single to left field that drove in Brandon Crawford and Solano.

San Francisco Giants' Kevin Pillar, right, slides into home to score a run next to Oakland Athletics catcher Chris Herrmann during the second inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

San Francisco Giants' Kevin Pillar, right, slides into home to score a run next to Oakland Athletics catcher Chris Herrmann during the second inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Sam Coonrod (4-0) retired three batters to win. Will Smith pitched the ninth for his 30th save.

The Giants appeared to end the game when Josh Phegley singled with two outs and was called out at second base after a throw by center fielder Kevin Pillar. Following a brief review, the call was overturned and Phegley was ruled safe. Smith then got Marcus Semien to ground out.

Earlier in the game, Longoria got his 1,000th career RBI with a solo home run off starter Brett Anderson.

San Francisco Giants' Evan Longoria, right, is congratulated by Austin Slater after hitting a solo home run against the Oakland Athletics during the third inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

San Francisco Giants' Evan Longoria, right, is congratulated by Austin Slater after hitting a solo home run against the Oakland Athletics during the third inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

The Giants won three of four against their Bay Area rivals this season.

STARTING OFF

Neither starter factored in the decision. Anderson allowed six hits and three runs in six innings, while Webb gave up four runs and six hits in 4 2/3 innings.

Oakland Athletics' Mark Canha, right, is congratulated by third base coach Matt Williams after hitting a solo home run against the San Francisco Giants during the fourth inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Oakland Athletics' Mark Canha, right, is congratulated by third base coach Matt Williams after hitting a solo home run against the San Francisco Giants during the fourth inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

CANHA'S DAY

Canha had an eventful day at the plate and in the field. In addition to his second career multihomer game, he made a terrific catch at the wall in center field but later made a soft throw back to the infield on a sacrifice fly in the second and let Solano's single get past him for an error in the fourth.

NEAR MISS

San Francisco Giants shortstop Brandon Crawford fields a groundout by Oakland Athletics' Robbie Grossman during the first inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

San Francisco Giants shortstop Brandon Crawford fields a groundout by Oakland Athletics' Robbie Grossman during the first inning of a baseball game in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Stephen Vogt, who hit a three-run homer as part of the Giants' eight-run eighth inning Saturday night, narrowly missed going deep again twice during the same at-bat in the sixth. Vogt hit a deep fly that curled just outside the left field foul pole, then came up short when Chad Pinder made a leaping grab at the fence.

PROFAR AT DH

Jurickson Profar had a double and two walks as the A's designated hitter. Profar, who began the season as Oakland's starting second baseman, replaced slumping slugger Khris Davis, who grounded out as a pinch-hitter in the ninth and is in a 1-for-29 funk.

Former Oakland Athletics player Rickey Henderson, left, gestures next to former teammate Dave Stewart before a baseball game between the Athletics and the San Francisco Giants in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Former Oakland Athletics player Rickey Henderson, left, gestures next to former teammate Dave Stewart before a baseball game between the Athletics and the San Francisco Giants in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

HONORING '89 CHAMPS

Members of the A's 1989 World Series championship team were honored during a pregame ceremony. Several members including Hall of Famer Rickey Henderson and manager Tony La Russa attended the event. Dave Stewart, who won two games during the earthquake-interrupted series, threw out the ceremonial first pitch to his former catcher, Terry Steinbach.

TRAINER'S ROOM

Former Oakland Athletics player Dave Stewart throws out the ceremonial first pitch before a baseball game between the Athletics and the San Francisco Giants in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Former Oakland Athletics player Dave Stewart throws out the ceremonial first pitch before a baseball game between the Athletics and the San Francisco Giants in Oakland, Calif., Sunday, Aug. 25, 2019. (AP PhotoJeff Chiu)

Giants: Johnny Cueto will make the first of two rehab starts for Triple-A Sacramento on Monday. Cueto has already made four minor league rehab starts as he works back from Tommy John surgery.

Athletics: C Chris Hermann left with a groin contusion after getting hit below the belt by a bounced pitch. Phegley replaced Hermann. . Stephen Piscotty is wearing a walking boot on his right foot and it doesn't appear the outfielder will be back in the lineup anytime soon. Piscotty aggravated an ankle sprain during an at-bat Saturday night. "He had an MRI this morning. We're waiting on the results from that," manager Bob Melvin said. "I don't have a great feeling about it at this point but we'll see what the doctors have to say and then proceed accordingly."

UP NEXT

Giants: RHP Tyler Beede (3-7, 5.82 ERA) pitches against the Arizona Diamondbacks for the third time this season Monday in San Francisco.

Athletics: Homer Bailey (11-8, 5.06) faces the Royals for the first time in his career Monday at Kauffman Stadium. Bailey began the season with Kansas City before being traded to Oakland on July 14.

More AP MLB: https://apnews.com/tag/MLB and https://twitter.com/AP_Sports