我的数据库中有两个表。一个是 jobs
,另一个是 type_of_service
,其中绑定(bind)了列 case_reference
和 ref_number
,这意味着对于每个案例 case_reference相同的数字出现在 ref_number 中,它是唯一的,并且以自定义方式自动增加,它们看起来像:
JOBS
==========
case_reference | customer | vessel
===============|==========|========
1311/2 | Dave | rg8
1311/3 | Billy | fjg32
1311/4 | Alex | sh599
和:
TYPE_OF_SERVICE
===============
ref_number | one | two | three | four
========================================
1311/2 |fire | | | medical
1311/3 | |foam | |
1311/4 | |foam |engine | medical
现在在我的网络应用程序中,我想以如下方式显示搜索结果:
reference | customer | vessel | type of service
================================================
1311/2 | Dave | rg8 | fire-medical
1311/3 | Billy | fjg32 | foam
1311/4 | Alex | sh599 | foam-engine-medical
我得到结果的方法是:
$记录=数组();
// build array of field names=============================================================================
$fields=array('customer','vessel',
'one','two','three','four');
// initialize empty array for WHERE clauses
$wheres=array();
// loop through field names, get POSTed values,
// and build array of WHERE clauses, excluding false values
foreach ($fields as $field) {
// get existing field value from POST, mark missing or empty value as FALSE
${$field} = isset($_POST[$field]) && trim($_POST[$field])!=''
? trim($_POST[$field]) : false;
// add to array of WHERE clauses only if value is not FALSE
if (${$field}) { $wheres[]="$field LIKE '%".${$field}."%'"; }
}
// build SELECT statement from WHERE clauses
$sql="SELECT * FROM jobs,services WHERE ref_number = case_reference AND ".
(!empty($wheres) ? implode(" AND ",$wheres) : '1=1').
";";
以及显示它们的方法:
<tbody>
<?php
foreach($records as $r) {
$watson = $r->case_reference;
?>
<tr>
<td><?php echo escape($r->case_reference); ?></td>
<td><?php echo escape($r->customer); ?></td>
<td><?php echo escape($r->vessel); ?></td>
<td><?php for ($i=0; $i <= $found; $i++) { echo escape(<?php echo '<pre>'.$records[$i]->one.'-'.$records[$i]->two.'-'.$records[$i]->three.'-'.
$records[$i]->four'</pre>'} ?>); ?></td>
</tr>
<?php
}
?>
</tbody>
但我遇到的问题是,当我尝试这样做时,会发生以下结果:
reference | customer | vessel | type of service
================================================
1311/2 | Dave | rg8 | fire---medical-,
1311/3 | Billy | fjg32 | -foam---
1311/4 | Alex | sh599 | -foam-engine-medical-
是否有一种方法/功能可以实现所需的效果,而不显示额外的 "-"
或表 type_of_service
中空单元格的额外空格?
最佳答案
您可以使用CONCAT_WS
来简化它
mysql> SELECT CONCAT_WS(',','First name','Second name','Last Name');
-> 'First name,Second name,Last Name'
mysql> SELECT CONCAT_WS(',','First name',NULL,'Last Name');
-> 'First name,Last Name'
因此 SQL 查询将是
SELECT a.case_reference AS reference,
a.customer AS customer,
a.vessel AS vessel,
CONCAT_WS('-',b.one,b.two,b.three,b.four) AS types_of_service
FROM JOBS a,
TYPES_OF_SERVICE b
WHERE a.case_reference = b.ref_number;
如您所见,如果没有值,我假设该字段包含 NULL
ref_number | one | two | three | four
========================================
1311/2 |fire | NULL| NULL | medical
如果不是这种情况,则需要一些额外的工作。
关于MySQL转义空/空记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395430/