简体   繁体   English

MYSQL选择并加入结果

[英]MYSQL Select and joining the results

Hi I am working on a search for a little system that we use in work. 嗨,我正在寻找一个我们在工作中使用的小系统。 The systems uses 3 tables, quotes, jobs and details. 系统使用3个表,报价,作业和详细信息。 within these tables there are several fields but th eone im focusing on are as follows: 在这些表中有几个字段,但我要重点关注的领域如下:

Here is a sqlfiddle for my table and mysql statement 这是我的表和mysql语句的sqlfiddle

Quotes 行情

  • quote_id quote_id
  • quotes_title quotes_title

Jobs 工作

  • job_id job_id
  • job_quoteid - job_quoteid stores the id of a quote which the job is realted to job_quoteid- job_quoteid存储要实现作业的报价的ID

Details 细节

  • details_id details_id
  • details_line details_line
  • details_quoteid - details_quoteid stores the id of a quote which the detail relates too, there can be several details linked to the quote details_quoteid- details_quoteid也存储与该细节相关的报价的ID,可以有多个链接到该报价的细节

My search is then to look at each of the tables based on my search term that is typed in and display the quote_id, job_id, quote_title and details_line. 然后,我的搜索是根据输入的搜索词查看每个表,并显示quote_id,job_id,quote_title和details_line。

here is the code i have currently for my search: 这是我目前用于搜索的代码:

$fetch = mysql_query("SELECT * FROM quotes AS qts 
                      LEFT JOIN jobs AS jbs ON qts.quote_id=jbs.job_quoteid
                      JOIN details AS dts ON qts.quote_id=dts.quote_detailsid

                      WHERE concat(' ',qts.quote_code,qts.quote_id) like '%$param%'
                      OR qts.quote_title REGEXP '^$param'

                      OR concat(' ',jbs.job_code,jbs.job_id) like '%$param%'

                      OR dts.details_line LIKE '%$param%'
                     ");

while ( $row = mysql_fetch_object( $fetch ) ) {
    $sResults .= '<tr id="'. $row->job_id . '">';
    $sResults .= '<td><a href="../../jobs.php?action=viewJob&amp;job_id='. $row->job_id .'">'. $row->job_code . $row->job_id .'</a></td>';
    $sResults .= '<td><a href="../../quotes.php?action=viewQuote&amp;quote_id='. $row->quote_id .'">' . 'Q' . $row->quote_id . '</a></td>';
    $sResults .= '<td>' . $row->quote_title . '</td>';
    $sResults .= '<td>' . $row->details_line . '</td>
    </tr>';
}

Now this is where I need some further assistants, the search works, but for example if i was to type in "NCR" as a search term it will show 4 results, this is because I have a quote_title of "NCR Sets" and this quote has 4 detail_line attahced to it (these detail_line dont contain the words NCR) but obvioulsy its displaying 4 because it has found that the quote_id that contains the word "NCR" is in each of those. 现在这是我需要其他助手的地方,搜索有效,但是例如,如果我输入“ NCR”作为搜索词,它将显示4个结果,这是因为我的quote_title为“ NCR Sets”,并且quote附加有4个detail_line(这些detail_line不包含单词NCR),但由于它发现每个单词中都包含包含单词“ NCR”的quote_id,所以它的显示为4。 My results page therefore has the 4 results each in a table row with the quote_id, job_id, job_title and detail_line being diplayed, what I would like it to look is to have the 4 details display under one job_id and quote_id. 因此,我的结果页在一个表行中每个都有4个结果,并且显示了quote_id,job_id,job_title和detail_line,我希望它看起来是在一个job_id和quote_id下显示4个详细信息。 If i was to code the table manually I would do somehting like the following to give you an idea: 如果我要手动对表进行编码,我会做类似以下的事情来给你一个主意:

<table>
 <tr>
    <td>quote_id</td>
    <td>job_id</td>
 </tr>
 <tr>
    <td>quote_title</td>
 </tr>
 <tr>
    <td>details_line</td>
    <td>details_line</td>
    <td>details_line</td>
    <td>details_line</td>
 </tr>
</table>

I hope this makes sense and some one can push me in the right direction. 我希望这是有道理的,并且有人可以将我推向正确的方向。

Thanks in advance. 提前致谢。

Ian 伊恩

The easiest way to do that could be to store the all outputs from the query, ie $row->job_id , $row->quote_id and so on in an array (or a variable) and check to see if the next job_id or quote_id is unique. 最简单的方法是将查询的所有输出(即$row->job_id$row->quote_id等)存储在数组(或变量)中,然后检查下一个job_id还是quote_id是独特的。 To do this, you can use the in_array() function. 为此,您可以使用in_array()函数。

You can then set up an if statement to append a td inside the existing tr if the new id already exists or else just add a new tr . 然后,您可以设置if语句,如果新的id已经存在,则将td附加到现有的tr ,否则只需添加新的tr

I could try and write a working code for it should you wish for it. 如果您愿意,我可以尝试为它编写一个有效的代码。 For that, a Fiddle would be easy! 为此,提琴很容易!

Try using this hope this might help. 尝试使用这种希望对您有所帮助的希望。

SELECT * FROM quotes AS qts 
Inner JOIN jobs AS jbs ON qts.quote_id=jbs.job_quoteid
Inner JOIN details AS dts ON qts.quote_id=dts.quote_detailsid

WHERE concat(' ',qts.quote_code,qts.quote_id) like '%NCR Sets%'
OR qts.quote_title REGEXP '^NCR Sets'

OR concat(' ',jbs.job_code,jbs.job_id) like '%NCR Sets%'

OR dts.details_line LIKE '%NCR Sets%'

group by qts.quote_id

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

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