尝试检索插入预订表的最后一个 ID,但收到“子查询返回多于 1 行”错误消息
本文关键字:返回 查询 消息 错误 插入 检索 ID 最后一个 | 更新日期: 2023-09-27 18:33:54
我正在尝试检索插入到特定表中的最后一个id,以便我可以在另一个表中使用它,但不断收到以下错误消息:
更新 1
其他信息:您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册以获取正确的 在"选择 last_insert_id() FROM booking)"、"0"、"MK12"附近使用的语法 5DR,162,温莎街,伍尔弗' 在 1 号线
// Open connection and insert booking
string query =
"INSERT INTO booking (operator_id, plot_id, postcode, datetime, stops, mileage, price, passengers, name, note, phone, status) VALUES ('" +
_operatorId + "', '" +
_plotId + "', '" + _postcode + "', '" + _datetime + "', '" + _stops + "', '" + _mileage + "', '" +
_price + "', '" + _passengers + "', '" + _name + "', '" + _note + "', '" + _phone + "', '" + Status +
"');";
for (int i = 0; i < _waypointList.Count; i++)
{
query +=
"INSERT INTO waypoint (booking_id, sequence, address, lat, lng) VALUES (select last_insert_id() FROM booking)" +
"', '" +
i + "', '" + _waypointList[i] + "', '" + _lat + "', '" + _lng + "');";
}
var dbObject = new DbConnect();
dbObject.InsertBooking(query);
更新 2
进行了一些更改:
// Open connection and insert booking
string query =
"INSERT INTO booking (operator_id, plot_id, postcode, datetime, stops, mileage, price, passengers, name, note, phone, status) VALUES ('" +
_operatorId + "', '" +
_plotId + "', '" + _postcode + "', '" + _datetime + "', '" + _stops + "', '" + _mileage + "', '" +
_price + "', '" + _passengers + "', '" + _name + "', '" + _note + "', '" + _phone + "', '" + Status +
"');";
for (int i = 0; i < _waypointList.Count; i++)
{
query +=
"INSERT INTO waypoint (booking_id, sequence, address, lat, lng) VALUES ((select last_insert_id() FROM booking), '" + i + "', '" + _waypointList[i] + "', '" + _lat + "', '" + _lng + "');";
}
var dbObject = new DbConnect();
dbObject.InsertBooking(query);
现在我收到以下错误消息:
子查询返回多于 1 行
知道我该如何解决它吗?
试试这个:
// Open connection and insert booking
string query =
"INSERT INTO booking (operator_id, plot_id, postcode, datetime, stops, mileage, price, passengers, name, note, phone, status) VALUES ('" +
_operatorId + "', '" +
_plotId + "', '" + _postcode + "', '" + _datetime + "', '" + _stops + "', '" + _mileage + "', '" +
_price + "', '" + _passengers + "', '" + _name + "', '" + _note + "', '" + _phone + "', '" + Status +
"');";
for (int i = 0; i < _waypointList.Count; i++)
{
query +=
"INSERT INTO waypoint (booking_id, sequence, address, lat, lng) VALUES ((select MAX(last_insert_id()) FROM booking), '" + i + "', '" + _waypointList[i] + "', '" + _lat + "', '" + _lng + "');";
}
var dbObject = new DbConnect();
dbObject.InsertBooking(query);