简体   繁体   English

批处理执行的SQL语句可更新两个表

[英]SQL statement with batch execution to update two tables

I have a problem which seems difficult for me, I have a table Category and a table Questions(this table has category_id field with default 0), On my form I can select a category from combobox and my question to you, how can I Insert/Update to a table a new question to a ready made category? 我有一个对我来说似乎很困难的问题,我有一个表Category和一个表Questions(该表的category_id字段默认为0),在我的表单上我可以从组合框中选择一个类别,然后向我提问,如何插入/将新的问题更新到表格中以获取现成的类别? My code so far Only adds a new question with a 0 as category, not the one from combobox (combobox displays only text) 到目前为止,我的代码仅添加了一个新问题,类别为0,而不是组合框(组合框仅显示文本)

String SQL2 = "UPDATE gamequestions SET question_id='gamecategory.category_name =" +jComboBox1.getSelectedItem().toString()+" ,gamequestions.category_id = gamecategory.category_id' WHERE question_id='0' ;"; 字符串SQL2 =“ UPDATE gamequestions SET question_id ='gamecategory.category_name =” + jComboBox1.getSelectedItem()。toString()+“,gamequestions.category_id = gamecategory.category_id'Where疑问ID ='0';”; stmt.addBatch(SQL2); stmt.addBatch(SQL2);

This is the updated code 这是更新的代码

"INSERT INTO gamequestions (question_id, category_id, question) SELECT NULL as question_id ,c.category_id, q.question='"+jTextArea1.getText()+"' FROM gamecategory c, gamequestions q WHERE  c.category_name = '"+Combo_click+"' ORDER BY c.category_id LIMIT 1"

And I cant figure out how to make jtextarea fill a column question. 而且我不知道如何使jtextarea填充列问题。

I can't figure out what your code is supposed to be doing. 我不知道您的代码应该做什么。

You say you want to either INSERT a new row with a specified value for category_id column (something other than a hard coded zero), or you want to UPDATE an existing row to change the value of the category_id column. 您说要插入一个新行并为category_id列指定值(不是硬编码零),或者想要更新现有行以更改category_id列的值。

Let's start with the SQL text that would needs to be sent to the database. 让我们从需要发送到数据库的SQL文本开始。

To change an existing row, for example: 要更改现有行,例如:

UPDATE gamequestions q
   SET q.category_id = 2 
 WHERE q.question_id = 42

To insert a new row (assuming question_id is auto_increment) for example: 例如,要插入新行(假设question_id为auto_increment):

INSERT INTO gamequestions (question_id, category_id) VALUES (NULL, 2)

The value returned from the combobox selection would replace the '2' in the example above. 从组合框选择中返回的值将替换上例中的“ 2”。

If your combobox isn't returning the category_id value, but is returning the value of some other column from the gamecategory table, and you want the SQL statement to do a "lookup" of the category_id value from the gamecategory table using the category_name value, one way to do that is a subquery as the value being assigned to the column...: 如果你的组合框没有返回category_id值,但返回从一些列的值gamecategory表,你想要的SQL语句做了“查找” category_id从价值gamecategory使用表category_name值,一种方法是将子查询作为值分配给列...:

UPDATE gamequestions q
   SET q.category_id = (SELECT c.category_id 
                          FROM gamecategory c
                         WHERE c.category_name = ?
                         ORDER BY c.category_id
                         LIMIT 1
                       )
 WHERE q.question_id = 42

For an INSERT , if you only want to insert a row if the category_name text matches a row in the gamecategory table, you could use SQL like this: 对于INSERT ,如果您只想在category_name文本与gamecategory表中的行匹配时插入一行,则可以使用如下SQL:

INSERT INTO gamequestions (question_id, category_id)
SELECT NULL as question_id
     , c.category_id
  FROM gamecategory c
 WHERE c.category_name = ?
 ORDER BY c.category_id
 LIMIT 1

If there is no row in the gamecategory table with a category_name that matches the specified value (supplied where the ? is), then no row will be inserted. 如果gamecategory category_name表中没有category_name与指定值匹配的行(在?处提供),则不会插入任何行。

If you want to go ahead and insert the row, even if the category_name text doesn't match, and have the category_id set to a default value of 0, then something like this, with a subquery in the SELECT list: 如果您要继续插入行,即使category_name文本不匹配,并且将category_id设置为默认值0,则可以在SELECT列表中使用子查询进行如下操作:

INSERT INTO gamequestions (question_id, category_id)
SELECT NULL as question_id
     , IFNULL(
         ( SELECT c.category_id
             FROM category c
            WHERE c.category_name = ?
            ORDER BY c.category_id
            LIMIT 1
         )
       ,0) AS category_id

The first step is really to get some SQL statements that do what you want. 第一步实际上是获得一些可以执行所需操作的SQL语句。

The second step is to get the SQL text embedded into your application, with the appropriate bind variables/substitutions and avoiding SQL injection vulnerabilities. 第二步是将SQL文本嵌入到您的应用程序中,并带有适当的绑定变量/替换,并避免SQL注入漏洞。 The second step is hard enough to figure out, without also having to work out the first step at the same time. 第二步很难找出,而不必同时制定第一步。


FOLLOWUP 跟进

If you are inserting a row into gamequestions , we'd expect the value for the question column to be supplied in the INSERT statement, it wouldn't be coming from a row in the gamequestions table. 如果您要在gamequestions中插入一行,我们希望在INSERT语句中提供question列的值,那么它就不会来自gamequestions表中的gamequestions

The SELECT we're running as part of the INSERT is getting the value of category_id from the gamecategory table; 我们正在作为INSERT的一部分运行的SELECT是从gamecategory表中获取category_id的值。 there's no need for a JOIN to the gamequestions table. 无需加入gamequestions表中。

I think you want SQL of the form: 我认为您需要以下形式的SQL:

 INSERT INTO gamequestions
 ( question_id
 , category_id
 , question
 , another_column
 , some_id
 )
 SELECT NULL
      , c.category_id       
      , 'my question text goes here'
      , 'fee fi fo fum'
      , 42
   FROM gamecategory c
  WHERE c.category_name = 'category name text goes here'
  ORDER BY c.category_id
  LIMIT 1

That will insert a new row, with the value of 这将插入一个新行,其值为

  • some_id set to integer value of 42 some_id设置为42的整数
  • another_column set to a string value of 'fee fi fo fum' another_column设置为'fee fi fo fum'的字符串值
  • question set to a string value of 'my question text goes here' question设置为'my question text goes here'的字符串值
  • category_id set to a value retrieved from the gamecategory table category_id设置为从gamecategory表中检索到的值

Ideally, you wouldn't need to do a lookup of the category_id from the database at all; 理想情况下,您根本不需要从数据库中查找category_id。 ideally, your combo box would display the category_name , but return a value of category_id , so you could just directly insert that: 理想情况下,您的组合框将显示category_name ,但返回category_id的值,因此您可以直接将其插入:

 INSERT INTO gamequestions
 ( question_id
 , category_id
 , question
 , another_column
 , some_id
 ) VALUES
 ( NULL
 , 5
 , 'my question text goes here'
 , 'fee fi fo fum'
 , 42
 )

without having to run a SELECT on the insert. 无需在插入文件上运行SELECT。

Ideally, instead of returning the category_name text as a value, the combo box would return the appropriate category_id value. 理想情况下,组合框将返回适当的category_id值,而不是返回category_name文本作为值。 Put the "lookup" into the drop down 将“查找”放入下拉菜单

For example, represent the gamecategory table contents: 例如,代表gamecategory表内容:

id  category_name
--  ---------------
 0  Uncategorized
 1  Action-Adventure
 2  Adventure
 3  Aerial Combat
 4  Fighting
 5  First Person Shooter
 6  Fitness

In an HTML combobox like this: 在这样的HTML组合框中:

 <select name=category_id>
 <option value='0' selected>Uncategorized</option>
 <option value='1'>Action-Adventure      </option>
 <option value='2'>Adventure             </option>
 <option value='3'>Aerial Combat         </option>
 <option value='4'>Fighting              </option>
 <option value='5'>First Person Shooter  </option>
 <option value='6'>Fitness               </option>
 </select>

To get the value from the combobox in javascript, for example: 要从javascript的组合框中获取值,例如:

$('select[name="category_id"]').val();

FOLLOWUP 跟进

The question_id column could be omitted from the INSERT statement, then we wouldn't need to supply the NULL value for it. 可以从INSERT语句中省略question_id列,然后我们不需要为其提供NULL值。

 INSERT INTO gamequestions (category_id, question, another_column)
 VALUES (5, 'my question text goes here', 'fee fi fo fum');

-or- -要么-

 INSERT INTO gamequestions (category_id, question, another_column)
 SELECT 5, 'my question text goes here', 'fee fi fo fum'

FOLLOWUP 跟进

Q: what if I was to insert multiple rows into question column while using INSERT INTO gamequestions, so I can have ie 3 textfields each containing different question but each going to same game category INSERT INTO gamequestions ( question_id , category_id , question , another_column , some_id ) VALUES ( NULL , 5 , 'my question text goes here' , 'fee fi fo fum' , 42 ) 问:如果我在使用INSERT INTO游戏问题时在问题列中插入多行怎么办,那么我可以拥有3个文本字段,每个文本字段包含不同的问题,但每个文本字段都属于同一游戏类别INSERT INTO游戏问题(question_id,category_id,question,another_column,some_id )值(NULL,5,'我的问题文本在这里','fee fi fofum',42)

A: Using the VALUES clause, you can do a multi-row insert (a very powerful and non-standard extension provided by MySQL). 答:使用VALUES子句,您可以进行多行插入(MySQL提供的功能非常强大且非标准的扩展)。

The SQL that you would send to the database would need to look something like this: 您要发送到数据库的SQL需要看起来像这样:

INSERT INTO gamequestions (category_id,question,another_column,some_id)
VALUES 
 (5, 'my question text goes here'  , 'fee fi fo fum'     , 42)
,(5, 'another question to be added', 'i smell the blood' , 84)
,(5, 'and this is a third row'     , 'of an englishman'  , 17)

(I've omitted the question_id column, we'll let that be assigned the DEFAULT NULL value and the real value will be assigned by AUTO_INCREMENT. Note the column is still being "inserted" along with the rest of the row, we're just giving an incomplete list of columns, and letting MySQL assign default values for the columns we don't specify.) (我省略了question_id列,我们将其分配为DEFAULT NULL值,而实际值将由AUTO_INCREMENT分配。请注意,该列仍与该行的其余部分一起被“插入”,仅给出不完整的列列表,并让MySQL为我们未指定的列分配默认值。)

The real "trick" here is that we have three rows, three sets of values. 真正的“技巧”是,我们有三行,三组值。 The set of values for each row is enclosed in parens, just like in the single row insert. 每一行的值集都用括号括起来,就像在单行插入中一样。 But MySQL allows us to tack on a comma, and another set of values for another row. 但是MySQL允许我们加上逗号,为另一行加上另一组值。

The category_id assigned on each row has to be individually supplied for each row, it can be the same value as previous rows, or entirely different, doesn't make any difference. 必须为每行分别提供在每行上分配的category_id,它可以与前几行相同,也可以完全不同,不会有任何区别。 (The only difference the values make is whether MySQL is going to throw an error, because of a constraint violation, a problem with converting datatypes, something like that.) (值的唯一区别是,由于违反约束,转换数据类型的问题等原因,MySQL是否将引发错误。)

(MySQL ignores all that spacing, I just added the spacing to make it more readable.) (MySQL忽略了所有间距,我只是添加了间距以使其更具可读性。)

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

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