NEW YORK (AP)  It took almost eight innings for the San Diego Padres to squeeze out their first hit Sunday, and only when Alexei Ramirez’s sharp grounder barely stayed fair.

If not for that, who knows?

Steven Matz carried a no-hit bid into the eighth and the New York Mets beat the Padres 5-1 to win consecutive games for the first time in more than five weeks.

“I could tell he had a feel for just about everything today,” San Diego manager Andy Green said. “He’s down 3-1 in a few counts, he’s flipping in curveballs. Fastball up, life through the zone. He was down when he wanted to be; he located well. We didn’t do much of anything against him and I don’t think until Alexei hit that ball we really squared anything up at all. So I think it was just one of those days where he was substantially better than us.”

Ramirez slapped a one-out single just inside first base on Matz’s 105th and final pitch in 96-degree heat at Citi Field. The ball appeared to skip right over the bag, and first base umpire Carlos Torres quickly called it fair.

“Guess it was a coin flip,” Mets second baseman Neil Walker said. “He absolutely had no-hit stuff today. You could tell that early in the game. … He’ll have another shot at it.”

Walker and Wilmer Flores each hit a solo homer off lefty Clayton Richard (0-2) as San Diego began its shift to a six-man rotation during a stretch of 16 games between off days.

“As the game went on, I got a little bit better,” said Richard, a former backup quarterback at Michigan who was a regular starter for the Padres from 2009-13. “I felt good pretty much all the way through.”

Richard allowed two runs over five innings in his first major league start since Aug. 2, 2015, for the Chicago Cubs.

“I couldn’t ask for anything more,” Green said. “He was outstanding.”

Facing the Padres for the first time, Matz (9-8) earned just his second victory in nine decisions over his past 14 starts.

The 25-year-old lefty, a hometown favorite who grew up about 50 miles from Citi Field, struck out eight and walked two. Mixing his 94-95 mph fastball with a sharp curve, he fanned All-Star slugger Wil Myers all three times.

“I think he just had everything working for him today,” Ramirez said through an interpreter. “Everything was moving.”

The lone Mets pitcher to throw a no-hitter was Johan Santana in June 2012 against St. Louis.

San Diego is the only major league team without one.

Struggling to stay close in the wild-card race, the Mets (59-58) took two of three in the series. The defending NL champions hadn’t strung together two wins since July 6-7 against Miami and Washington.

The Mets added three insurance runs in the eighth, when Jose Reyes scored on a wild pitch and rookie T.J. Rivera  another New York product playing for his hometown team  hit a two-run double for his first career RBIs.

Addison Reed and Gabriel Ynoa finished the four-hitter. Ynoa yielded Yangervis Solarte’s run-scoring single in the ninth, but the Padres fell to 10-28 in day games.

Before Ramirez’s single, the closest the Padres came to a hit was Solarte’s line drive right back to Matz with two outs in the fourth. Matz put up his glove and almost casually caught the ball before Solarte slammed down his bat in frustration.

“The only thing on my mind was getting outs,” Matz said.

MAYBE THIS MIGHT WORK

Padres leadoff man Travis Jankowski, who struggles against left-handed pitching, tried to bunt for a hit with a runner on first and two outs in the sixth. Jankowski missed his attempt for strike one and grounded out to end the inning.

TRAINER’S ROOM

Padres: Rookie 2B Ryan Schimpf was rested but won’t sit out against every lefty, Green said. Schimpf had a pinch-hit single off Reed in the eighth and stayed in the game. … CF Jon Jay (broken right forearm) is scheduled to have another CT scan Monday in California, and the team hopes he will be cleared to begin swinging a bat. … … RHP Colin Rea (right elbow) does not have a date for surgery yet. He will see noted orthopedist Dr. James Andrews on Wednesday, Green said.