我正在构建一个用户可以回答问题的Web应用程序。我试图运行一个查询,将这些答案插入到表格中,但用户只能回复同一个问题。在我的情况下,查询必须检查question_id&的重复项。 reply_user。 (reply_id已被定义为主键)。
例如,当我在表格中有答案时:question_id = 1& reply_user = John,John无法在question_id 1上回复。但是另一个用户当然可以。
我目前正在运行这个:
INSERT INTO replies (question_id, reply_user, reply_content, reply_anwer)
VALUES (:questionid, :replyuser, :replycontent, :replyanswer)
SELECT question_id, reply_user FROM replies WHERE NOT EXISTS (
SELECT question_id FROM replies
WHERE question_id = question_id AND reply_user = reply_user
)
我尝试了不存在但我无法找到解决方案。
感谢您的帮助
答案 0 :(得分:1)
您需要"""Import."""
import tensorflow as tf
import cv2
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[512, 512])
y_ = tf.placeholder(tf.float32, shape=[1, 1])
def get_image_from_file(file_name):
"""Function get_image_from_file."""
return cv2.resize(cv2.imread(file_name, 0), (512, 512),
interpolation=cv2.INTER_CUBIC)
def weight_variable(shape):
"""Foo."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
"""Foo."""
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, w):
"""Foo."""
return tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
"""Max pool 2x2."""
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
if __name__ == "__main__":
# 1st layer
w_conv1 = weight_variable([5, 5, 1, 16])
b_conv1 = bias_variable([16])
x_image = tf.reshape(x, [-1, 512, 512, 1])
h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
# 2nd layer
w_conv2 = weight_variable([5, 5, 16, 32])
b_conv2 = bias_variable([32])
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
# connection layer
w_fc1 = weight_variable([32 * 32 * 64, 128])
b_fc1 = bias_variable([128])
h_pool2_flat = tf.reshape(h_pool2, [-1, 32 * 32 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
# zapobieganie przeuczeniu
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# output layer
w_fc2 = weight_variable([128, 1])
b_fc2 = bias_variable([1])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, w_fc2) + b_fc2)
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.initialize_all_variables())
for i in range(24):
img = get_image_from_file("./MOJE/koty_nauka/kot" + str(i + 1) +
".jpg")
out = y_conv.eval(feed_dict={
x: img, y_: [[1]], keep_prob: 1.0})
print("----")
print(out)
2提交:
unique
然后使用此查询:
ALTER TABLE `replies` ADD UNIQUE (`question_id`,`reply_user`);
如果INSERT INTO replies (question_id, reply_user, reply_content, reply_anwer)
VALUES (:questionid, :replyuser, :replycontent, :replyanswer)
,question_id
存在查询,则不运行其他运行。
答案 1 :(得分:0)
在两个键上创建唯一的索引
ALTER TABLE replies ADD UNIQUE `preventDoubleAnswer` (`question_id`, `reply_user`) COMMENT '';
并将数据插入:
INSERT IGNORE ...
INSERT IGNORE
仅在满足唯一键时才会插入新数据。
注意:IGNORE
param被省略,它会抛出一个MYSQL错误