2010-02-10 69 views
0

我使用的是MySQL的INNER JOIN语句返回一个区分记录各种结果在主表与联接,类似于How to Join Multiple Joins yet return Distinct Values 我的查询是PHP + MySQL的连接语句安排mysql_fetch阵列中的多个值排

SELECT cl.*, dep.dept_name 
FROM epapers.clientelle cl 
INNER JOIN epapers.dept_staff_users depu 
ON depu.user_id=cl.user_id 
INNER JOIN epapers.dept dep 
ON dep.dept_id=depu.dept_id 
group by cl.user_id 
ORDER BY cl.user_name ASC, 

我想在下面

echo "<tr bgcolor='#CCCCCC'> 
<td >$num</td><td >".$row[user_id]."</td><td>".$row[user_name]."</td> 
<td >".$row[fname]."</td><td >".$row[lname]."</td> 
<td >".$row[dept_name]."</td> 
<td >".$row[dept_name]."</td> 
<td >".$row[dept_name]."</td> 
<td >".$row[email]."</td>"; 
$TrID = "$row[user_id]"; 
echo "<td >"; 
echo '<form name="activate" action="activate_acc.php" method="POST" ;">'; 
echo "<input type='hidden' name='TrID' value='$TrID'>";  
echo "<input type='checkbox' name='active' value='$row[active]'>"; 
echo '<input type="submit" name="Submit" value="Delete">'; 
echo '</form>'; 
echo "</td >"; 
... 

注意部门可高达3个系显示一个表来显示上述内容。如何将mysql查询结果以单行形式返回给部门?

回答

3

如果你正在使用MySQL,你可以使用GROUP_CONCAT但你必须做一些后期处理,这是一个有点难看。

SELECT cl.*, GROUP_CONCAT(DISTINCT dep.dept_name SEPARATOR '|') as dept_name 
FROM epapers.clientelle cl 
INNER JOIN epapers.dept_staff_users depu 
ON depu.user_id=cl.user_id 
INNER JOIN epapers.dept dep 
ON dep.dept_id=depu.dept_id 
group by cl.user_id 
ORDER BY cl.user_name ASC 



foreach(.... 
    $departments = explode('|', $row['dept_name']); 
... 
    if(isset($departments[0])) { 
     echo "<td>{$departments[0]}</td>"; 
    } else { 
     echo "<td></td>"; 
    } 
    // same check 
    echo "<td>{$departments[1]}</td>"; 
    // same check 
    echo "<td>{$departments[2]}</td>"; 

它有点在运行时denormalisation ...

+0

这个工作,但因为我是用'而($行= mysql_fetch_array($结果))'我不得不使用此: '.... 回声 “ $ NUM ” $行[USER_ID] “​​” $行[USER_NAME]“ 。。 “$行[FNAME]。” “$行[L-NAME]。”“; $部门=爆炸( '|',$行[ 'DEPT_NAME']); 如果(isset($部门[0])){ 回声 “​​{$部门[0]}”; }否则{ 回声 “​​”; } ....' – 2010-02-10 17:41:54

0

那么你可以用MySQL查询方式做到这一点是加入该表为三个部门,比如这个:选择a.dept_id作为dept_1 b.dept_id作为dept_2等,并为每个部门ID加入不同的表。这种方式虽然是处理器密集型的。按照写入查询的方式获取数据的速度要快得多,然后在使用数据之前使用函数处理数据。这里是我会怎么做一个简单的例子:

function prep_data($mysql_result) 
{  
    $results = array(); 
    $work = array(); 
    while ($row = mysql_fetch_assoc($mysql_result)) 
    { 
     if ($work['user_id'] == $row['user_id']) 
     { 
      // just add dept data to dept array 
      $dept[] = $row['dept_id']; 
     } 
     else 
     { 
      // data changed    
      if (isset($work['user_id'])) 
      { 
       // assign dept array to data array 
       $work['dept_id'] = $dept; 
       // add work array to results collection 
       array_push($results, $work);     
       // start working with the new user_id 
       $work = $row; 
       $dept = array($row['dept_id']); 
       continue;     
      } 
      else 
      { 
       // this is first pass. grab the row data and start the dept[] array; 
       $work = $row; 
       $dept = array($row['dept_id']); 
       continue; 
      } 
     } 
    } 
    $work['dept_id'] = $dept; 
    array_push($results, $work); 
    return($results); 
} 

//好运

+0

我决定用meouw的答案,因为它使用现在用的是相同的查询和更容易解释给我的客户。 – 2010-02-10 17:45:00