Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在MYSQL 5.1中使用“ORDER BY”和“LIMIT”语句进行多表更新_Sql_Mysql - Fatal编程技术网

如何在MYSQL 5.1中使用“ORDER BY”和“LIMIT”语句进行多表更新

如何在MYSQL 5.1中使用“ORDER BY”和“LIMIT”语句进行多表更新,sql,mysql,Sql,Mysql,我有两张桌子事件和固定地点 //////////////////////////////////////////////////////////////// CREATE TABLE ` fixedplace ` ( `idFixedPlacePrice` int(10) unsigned NOT NULL AUTO_INCREMENT, `NumberOfRunners` int(10) unsigned DEFAULT NULL, `Places` int(10) unsign

我有两张桌子事件和固定地点

////////////////////////////////////////////////////////////////

CREATE TABLE  ` fixedplace ` (
  `idFixedPlacePrice` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `NumberOfRunners` int(10) unsigned DEFAULT NULL,
  `Places` int(10) unsigned DEFAULT NULL,
  `FpOddsPrice` double DEFAULT NULL,
  PRIMARY KEY (`idFixedPlacePrice`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1;




Insert Into fixedplace (NumberOfRunners, Places, FpOddsPrice)
Values
(0, 0, 0),
(10, 3, 0.16),
(13, 4, 0.21);
//////////////////////////////////////////////////////////////

CREATE TABLE  ` fixedplace ` (
  `idFixedPlacePrice` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `NumberOfRunners` int(10) unsigned DEFAULT NULL,
  `Places` int(10) unsigned DEFAULT NULL,
  `FpOddsPrice` double DEFAULT NULL,
  PRIMARY KEY (`idFixedPlacePrice`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1;




Insert Into fixedplace (NumberOfRunners, Places, FpOddsPrice)
Values
(0, 0, 0),
(10, 3, 0.16),
(13, 4, 0.21);
要访问固定地点数据,我使用以下语句

SELECT
  Places,
  FpOddsPrice
FROM FixedPlace as 
WHERE NumberOfRunners <=  :FNumberOfRunners 
ORDER BY NumberOfRunners desc
LIMIT 1
i、 e.如果:FNumberOfRunners=11,则位置为3,FpOddsPrice=0.16

//////////////////////////////////////////////////////////////

CREATE TABLE  ` fixedplace ` (
  `idFixedPlacePrice` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `NumberOfRunners` int(10) unsigned DEFAULT NULL,
  `Places` int(10) unsigned DEFAULT NULL,
  `FpOddsPrice` double DEFAULT NULL,
  PRIMARY KEY (`idFixedPlacePrice`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1;




Insert Into fixedplace (NumberOfRunners, Places, FpOddsPrice)
Values
(0, 0, 0),
(10, 3, 0.16),
(13, 4, 0.21);
我在编写更新语句时遇到问题 将根据FixedPlace表中的值更新“事件”表

这行不通

UPDATE Events as E, FixedPlace as F 
Set E.Places       = F.Places,
    E.FpOddsPrice  = F.FpOddsPrice
WHERE E.idEvents = :FidEvents
   And F.NumberOfRunners <=  :FNumberOfRunners
ORDER BY F.NumberOfRunners desc
LIMIT 1
它给出了以下错误“不正确地使用更新订单方式”

我必须做些什么才能让它工作

编辑

这是可行的,但是有更好的方法吗

UPDATE Events as E
Set E.Places       = (Select Places     FROM FixedPlace WHERE NumberOfRunners <=  :FNumberOfRunners ORDER BY NumberOfRunners desc LIMIT 1),
    E.FpOddsPrice    = (Select FpOddsPrice FROM FixedPlace WHERE NumberOfRunners <=  :FNumberOfRunners ORDER BY NumberOfRunners desc LIMIT 1)
WHERE E.idEvents = :FidEvents
从文件中

对于多表语法,请更新 更新中命名的每个表中的行 表_满足以下条件的引用: 条件在本例中,按和进行订购 无法使用限制


这有点困难,因为MySQL不允许在子查询中使用LIMT

我要做的是两个独立的查询。第一个已经在你的岗位上了;第二个是简单的更新查询,如:

UPDATE Events E
SET  E.Places = :Places
,    E.FpOddsPrice = :Price
WHERE E.idEvents = :FidEvents

这两张桌子是如何连接的?“FixedPlace”是一个查找表,它的结果保存在事件表中。我仍然看不到它。在非工作更新查询中,您基本上是在进行交叉连接,我不确定这是您想要的。我丢失了某种外键。@LukášLalinský:MySQL也不允许在子查询中进行限制?但它允许在未命名的视图中进行限制,所以您可以更新foo SET。。。WHERE id IN SELECT id FROM SELECT foo_id FROM bar WHERE。。。限制1 aTwo查询将起作用-但出于性能原因-我想使用一个