简体   繁体   English

MySQL多表更新语句

[英]MySQL Multiple Table Update Statement

I am working on a study planner. 我正在研究学习计划者。 The SQL query below is supposed to run when a logged in user tries to save a study plan from the welcome page. 当登录的用户尝试从欢迎页面保存学习计划时,应运行以下SQL查询。 However, the code does not do anythin. 但是,该代码无法执行任何操作。 The input values just disappear upon submission with no initialisation of the $result variable, no flag changes and no any error print-outs when I run the application via NetBeans... 当我通过NetBeans运行应用程序时,输入值在提交后就消失了,没有初始化$result变量,没有标志更改,也没有任何错误打印输出...

// Set up a flag for monitoring the individual queries
    $flag = true;

    $query = "UPDATE
    `modulecodes`
    LEFT JOIN `moduletitles` ON `moduletitles`.`modulecodeid` = `modulecodes`.`modulecodeid`
    LEFT JOIN `studyplans` ON `studyplans`.`modulecodeid` = `modulecodes`.`modulecodeid`
    LEFT JOIN `comments` ON `comments`.`modulecodeid` = `modulecodes`.`modulecodeid`
    SET
    modulecode = '$moduleCode', moduletitle = '$moduleTitle', studydate = '$moduleStudyDate', numberofstudyhours = '$moduleAllocatedHours', comment = '$studyPlanComments'
    WHERE userid = '$userid';";

    // Execute the query and put the result in the variable $result
    $result = mysqli_query($mysqli, $query);
    if (!$result) {
        $flag = false;
        echo $flag;
        echo "Error details for Result: " . mysqli_error($mysqli) . ".";
    }

...And with hard-coded values (as per below) and direct test in phpMyAdmin, it runs but comes back with 0 rows affected. ...并使用硬编码的值(如下所述)并在phpMyAdmin中进行直接测试,它可以运行,但会返回0行。 (Query took 0.0069 seconds.) and no changes in the database, whatsoever. (查询花费0.0069秒。)并且数据库中没有任何更改。 So, I feel like I haven't structured it well. 所以,我觉得我的结构不够好。 Can someone kindly guide me on how to convert the query into one with a subquery or something more efficient and reliable, please or at the least, help point out what I am doing wrong? 有人可以请我指导如何将查询与子查询转换为更有效和更可靠的查询,或者至少请帮助我指出我做错了什么?

I'd suspect other parts of the code but the fact that a direct test in phpMyAdmin tells me, the issue is with the SQL statement, besides I have already used other parts of the code elsewhere and with another query and it runs fine. 我会怀疑代码的其他部分,但事实是phpMyAdmin中的直接测试告诉我,问题出在SQL语句,除了我已经在其他地方使用了代码的其他部分以及另一个查询,并且运行良好。

UPDATE
    `modulecodes`
    LEFT JOIN `moduletitles` ON `moduletitles`.`modulecodeid` = `modulecodes`.`modulecodeid`
    LEFT JOIN `studyplans` ON `studyplans`.`modulecodeid` = `modulecodes`.`modulecodeid`
    LEFT JOIN `comments` ON `comments`.`modulecodeid` = `modulecodes`.`modulecodeid`
    SET
    modulecode = 'P00100', moduletitle = 'Java', studydate = '2017/04/10', numberofstudyhours = '1', comment = 'Java'
    WHERE userid = 20;

Database Code: 数据库代码:

CREATE TABLE `comments` (
  `commentid` int(10) NOT NULL,
  `modulecodeid` int(10) NOT NULL,
  `comment` text,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


-- --------------------------------------------------------

--
-- Table structure for table `modulecodes`
--

CREATE TABLE `modulecodes` (
  `modulecodeid` int(10) NOT NULL,
  `userid` int(10) NOT NULL,
  `modulecode` varchar(10) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `moduletitles`
--

CREATE TABLE `moduletitles` (
  `moduletitleid` int(10) NOT NULL,
  `modulecodeid` int(10) NOT NULL,
  `moduletitle` varchar(100) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `studyplans`
--

CREATE TABLE `studyplans` (
  `studyplan` int(10) NOT NULL,
  `modulecodeid` int(10) NOT NULL,
  `studydate` date NOT NULL,
  `numberofstudyhours` int(10) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- --------------------------------------------------------


--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `userid` int(10) NOT NULL,
  `firstname` varchar(100) NOT NULL,
  `lastname` varchar(100) NOT NULL,
  `created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

To test the relationships, see if this returns the rows you expect to be updated: 要测试这些关系,请查看这是否返回您希望更新的行:

SELECT 
    a.`modulecode`, 
    b.`moduletitle`, 
    c.`studydate`, 
    c.`numberofstudyhours`, 
    d.`comment`
FROM `modulecodes` a
    LEFT JOIN `moduletitles` b ON b.`modulecodeid` = a.`modulecodeid`
    LEFT JOIN `studyplans` c ON c.`modulecodeid` = a.`modulecodeid`
    LEFT JOIN `comments` d ON d.`modulecodeid` = a.`modulecodeid`
WHERE `userid` = '$userid';

If any of the columns are NULL (joined table row not found), then the update will fail. 如果任何列为NULL(找不到联接表行),则更新将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM