PHP选择查询以比较来自不同mysql表的2个值(如果它们匹配)

时间:2019-04-16 18:54:18

标签: mysql

PHP新手参加了测验。在db 15 Questions / 15Answers中有一个输入表格,用于回答进入mysql表的答案。在数据库中还有一个问题表,其中的ID字段为15,正确的答案字段为

问题表:

  • Questions_ID-int 11 / PK /不为空
  • 问题-文本/不为空
  • 正确答案-文本/不为空

答案表:

  • 答案ID-int 11 / PK /不为空
  • inputanswer1-varchar 255 /不为空
  • inputanswer2-varchar 255 /不为空
  • inputanswer3-varchar 255 /不为空

我想知道使用这种方法从问题表中检查我在下面所做的正确答案,但是我遇到的问题是将其与存储输入答案的另一个表进行匹配。

问题是操作员用来说“等于”其他东西并查看它们是否匹配的意思。这就是所有的1选择查询。

问题表

SELECT fieldname (rightanswer) FROM tablename (Questions)  WHERE ID = 1;
SELECT fieldname FROM tablename WHERE Id = 2;

上面的代码正在工作,并且我要做的第一部分是从“问题”表中选择“正确答案”,第一个问题ID为“ 1”,现在需要第二部分匹配/比较的帮助以“ inputanswer1”开头的“答案”表中输入答案的位置

正在寻找“问题”表中的“正确答案”是否匹配“答案”表中的“ inputanswer1”。

???答案表

SELECT fieldname FROM tablename;

1 个答案:

答案 0 :(得分:0)

下面的查询假定您将问题表行与答案表相关联 每行使用ID列。但是您会在答案中显示ID列作为主键。

只需将ON子句编辑为正确的关系即可。

正如我在上面的评论中所提到的,问题表中的列import java.io.File; import java.io.IOException; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; public class UploadObject { public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String stringObjKeyName = "*** String object key name ***"; String fileObjKeyName = "*** File object key name ***"; String fileName = "*** Path to file to upload ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(clientRegion) .withCredentials(new ProfileCredentialsProvider()) .build(); // Upload a text string as a new object. s3Client.putObject(bucketName, stringObjKeyName, "Uploaded String Object"); // Upload a file as a new object with ContentType and title specified. PutObjectRequest request = new PutObjectRequest(bucketName, fileObjKeyName, new File(fileName)); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentType("plain/text"); metadata.addUserMetadata("x-amz-meta-title", "someTitle"); request.setMetadata(metadata); s3Client.putObject(request); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } } 的类型应与您将其进行比较的列的类型相同(IOW,varchar(255))

rightanswer

更新: OP提供的表架构

SELECT
    a.`ID`,
    a.`question`,
    a.`rightanswer`,
    b.`ID` as `aID`,
    b.`inputanswer1`,
    b.`inputanswer2`,
    b.`inputanswer3`,
FROM `questions` a 
JOIN `answers` b
ON a.`ID` = b.`ID`
WHERE a.`rightanswer` = b.`inputanswer1`