English 中文(简体)
查询结果的查询结果输出显示更多的行,然后它应该这样做
原标题:output of query result shows more rows then it should do

如果输入关键字, 您可以看到我在上面使用的查询 :

SELECT * FROM Question WHERE QuestionContent LIKE  %$each% ;

现在我在数据库里有25行, 它包含关键字“ 问题 ”, 所以如果我在搜索框中输入关键字“ 问题 ”, 我就会在输出中显示 25 行, 这是正确的 。

问题是,我内部需要将其他表格合并到查询中,因为今后我需要能够查看其他表格中字段的行,因此我将查询改为如下:

SELECT * FROM Answer a               
INNER JOIN Question q ON a.QuestionId = q.QuestionId
                 JOIN  Reply r ON q.ReplyId = r.ReplyId
                 WHERE QuestionContent LIKE  %$each% ;

现在,如果我再次搜索“问题”这个词, 我最终会得到125行显示在结果中,而不是25行, 为什么这样做,有人知道如何解决这个问题吗?

下面是问题表字段:

QuestionId, QuestionContent, ReplyId

以下是回答表格字段:

QuestionId, Answer

以下是回复表格字段:

ReplyId, ReplyType
最佳回答

很难用所提供的信息给出一个好答案, 但我猜每个问题都有多个答案,每个问题都有多个答案。

您需要思考您想要如何查询您的数据, 如果您真的想要每匹配一行, 那么您需要用 < code> gROUP by by 表示删除 < code> SELECT {/ code > 。

没有“ 固定 ”, 您需要设计一个最适合数据搜索方法的解决方案 。

但在您的 sql 查询中添加 < code> gROUP by 是一个良好的开始。

也请告诉我 $each 正在以 mysql_real_escape_string 逃跑

问题回答

如果每个问题都有多个答案(如我所想象的那样),每个答案都会有相同的答案。您可以使用 GROUP by SELECT DistinT 来进行当前查询,但当您想要每个答案中的东西时,这将会成为一个问题。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签