我有两张这样的桌子:
[tblFacilityHrs] id uid title description
[tblFacilityHrsDateTimes] id owner_uid startEventDate endEventDate startTime endTime days recurrence finalDate
他们通过UID处于一对多关系中:
我想加入这些表,以便只从tblFacilityHrsDateTimes获取ID值(这样当我编辑表时,第一个表tblFacilityHrs由UID编辑,tblFacilityHrsDateTimes由ID编辑)。
如何连接表以便以这种方式对其进行编辑?
像这样的:
<?php
include('../config.php');
if (isset($_GET['uid']) ) {
$uid = (int) $_GET['uid'];
$id = (int) $_GET['id'];
if (isset($_POST['submitted'])) {
foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); }
//Query for tblFacilityHrs
$sql = "`title` = '{$_POST['title']}' , `description` = '{$_POST['description']}' WHERE `uid` = '$uid' ";
mysql_query($sql) or die(mysql_error());
//Query for tblFacilityHrsDateTimes
$sql2 = "`startEventDate` = '{$_POST['startEventDate']}' , `endEventDate` = '{$_POST['endEventDate']}' , `startTime` = '{$_POST['startTime']}', `endTime` = '{$_POST['endTime']}' , `days` = '{$_POST['days']}' , `recurrence` = '{$_POST['recurrence']} , `finalDate` = '{$_POST['finalDate']}' WHERE `id` = '$id' ";
mysql_query($sql2) or die(mysql_error());
echo (mysql_affected_rows()) ? "Edited row.<br />" : "Nothing changed. <br />";
echo "<a href='list.php'>Back</a>";
}
$row = mysql_fetch_array (mysql_query("SELECT * FROM `tblFacilityHrs` WHERE `uid` = '$uid'"));
$row2 = mysql_fetch_array(mysql_query("SELECT * FROM `tblFacilityHrsDateTimes` WHERE `id` = '$id'"));
?>
在我的脑海中,我想象着桌子是这样连接的(但它不起作用):
$result = mysql_query("SELECT uid, title, description FROM tblFacilityHrs LEFT JOIN tblFacilityHrsDateTimes ON tblFacilityHrs.uid = tblFacilityHrsDateTimes.owner_uid ORDER BY tblFacilityHrs.description") or trigger_error(mysql_error());
最佳答案
(升级到答案)
您需要将tblFacilityHrsDateTimes
中的列选择到您的结果集中:
SELECT uid, title, description, tblFacilityHrsDateTimes.*
FROM tblFacilityHrs
LEFT JOIN tblFacilityHrsDateTimes
ON tblFacilityHrs.uid = tblFacilityHrsDateTimes.owner_uid
ORDER BY tblFacilityHrs.description
关于php - 表联接SQL/PHP(CRUD),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10503245/