douqiang5163 2012-05-08 17:13
浏览 41
已采纳

表连接SQL / PHP(CRUD)

I have two tables like this:

[tblFacilityHrs] id uid title description
[tblFacilityHrsDateTimes] id owner_uid startEventDate endEventDate startTime endTime days recurrence finalDate

They are in a one-to-many relationship by way of UID: enter image description here

enter image description here

I want to join the tables so that I only get the ID value from tblFacilityHrsDateTimes (so that when I edit the tables the first table tblFacilityHrs gets edited by UID and tblFacilityHrsDateTimes gets edited by ID).

How can I join the tables so they can be edited in this manner?

Something like this:

<?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'"));
?>

In my head I imagine the tables joining like this (but it doesn't work):

$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());
  • 写回答

1条回答 默认 最新

  • dsimib1625 2012-05-08 17:45
    关注

    (Upgrading to an answer)

    You need to select the columns from tblFacilityHrsDateTimes into your resultset:

    SELECT      uid, title, description, tblFacilityHrsDateTimes.*
    FROM        tblFacilityHrs
      LEFT JOIN tblFacilityHrsDateTimes
             ON tblFacilityHrs.uid = tblFacilityHrsDateTimes.owner_uid
    ORDER BY    tblFacilityHrs.description
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动