0

I have two databases. The first contains a list of upload doucuments and who they are for when uploaded and expiry date. This is stored as numbers representing each grade of person using implode. so column name 'foagrade' has 10,11,12 in row one. The second database is a list of grades and there ID's eg 10 = manager, 11 = HR etc. I am using left join and a basic html table to display a query.

$result = mysqli_query($con,
"SELECT UPLOAD.id, UPLOAD.title, UPLOAD.faoGrade,UPLOAD.faoLocation, UPLOAD.date,   UPLOAD.expiry, GRADE.ID, GRADE.GRADE as GR, GRADE.id
FROM UPLOAD
LEFT JOIN GRADE ON
UPLOAD.faoGrade=GRADE.ID
where owner=$user");
echo "<table id='previous'>
<tr>
<th>Title/Document name:</th>
<th>For attention of Grade</th>
<th>For Location</th>
<th>date</th>
<th>Date expires</th>
<th>Delete this briefing</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  $id=$row['id'];
  echo "<tr>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['GR'] . "</td>";
  echo "<td>" . $row['foaLocation']."</td>";
  echo "<td>" . date("D, d M Y ",($row['date']));
  echo "<td>" . date("D, d M Y ",($row['expiry']));
  echo "<td><a href=pages/login/upload_delete.php?item=".$id.">delete</a></td> ";
  echo "</tr>";
  }
echo "</table>";

The query shows all the details but only shows the first grade stored in the column faograde (MANAGER) not 11 or 12. How would I explode or split this? So for each number in this column a grade name is shown. i will also have to do the same for column 'faoLocation', but working on one issue at a time.

4

1 回答 1

0

这些表应该是规范化的,但是你已经有了你可以做这样的事情:

SELECT UPLOAD.id as uploadID,
UPLOAD.title,
UPLOAD.faoLocation,
UPLOAD.date,
UPLOAD.expiry,
GRADE.ID as gradeID1,
GRADE.GRADE as GR,
GRADE.id as gradeID2 FROM UPLOAD, GRADE
WHERE 
FIND_IN_SET(gradeID2,UPLOAD.faoGrade)
and UPLOAD.owner=$user;

更新 这里是SQL Fiddle

有多个名为id&的列有点令人困惑ID。最好给它们起直观的名称,例如grade_IDuser_ID。从规范化的角度来看,您可以使用关系表而不是逗号分隔的列(取决于此数据如何进入数据库)。这是一个关于规范化的视频

于 2013-05-04T09:17:16.223 回答