Beijing: At least seven students were stabbed to death and 12 injured in a knife attack outside a school in northern China on Friday, officials said.

The knife-wielding man attacked the students near Mizhi County Number Three secondary school in Shaanxi province as they were heading home for the day. The suspect was taken into police custody, Xinhua news agency reported.

Chinese police gave his surname as Zhao and said he was a former student of the school who took revenge because he was bullied when he studied there, according to a BBC report.

Photos on social media showed several young students lying on the ground, surrounded by terrified onlookers. A local government statement said the attack happened at the school at around 6.10pm.

China has seen several fatal knife attacks in recent years. A knifeman killed a woman and injured 12 others at a Beijing shopping centre in February.

In July 2017, two people died and nine were injured by a knife-wielding man at a Wal-Mart supermarket in Shenzhen.

The country executed three men in March 2015 after they were found guilty of a knife attack at Kunming station, where 29 people were hacked to death and over 100 others wounded.