【问题标题】:How does one go about deleting records from an array with Joins如何使用 Joins 从数组中删除记录
【发布时间】:2014-08-21 20:14:32
【问题描述】:

我有一个包含多个联接的查询。在我获取结果并通过 Id-checker 运行它之后,我希望能够从 IDDestination 等于 $ID 的那个数组中删除记录。

由于此查询具有联接,并且我基于其中一个联接表过滤它们,我如何根据该联接表从数组中删除这些记录?

我只希望在用户确认后发生这种情况。

$query = "
select d.IDCourse,
 d.name as course_name,
 d.slug,
 d.short_description,
d.address,
e.city_name,
e.state_code,
d.zip,
e.city_slug,
e.state_slug,
h.IDDestination,
LOWER(e.iso_code) as country_slug, a.*,
b.IDTeetimeType,
c.name as teetime_type,
b.start_time,b.end_time,

(case dayofweek(a.teetime_dt)
        when 1 then `b`.`sun`
        when 2 then `b`.`mon`
        when 3 then `b`.`tue`
        when 4 then `b`.`wed`
        when 5 then `b`.`thu`
        when 6 then `b`.`fri`
        when 7 then `b`.`sat`
    end) AS `price`, g.tax_rate, f.alias
from cart_course_teetimes a
join course_priceplan b
on a.IDCoursePricePlan = b.IDCoursePricePlan
join course_teetime_type c
on b.IDTeetimeType = c.IDTeetimeType
join course d
on b.IDCourse = d.IDCourse
join vw_cities e
on d.IDCity = e.IDCity
join destinations_cities h
on h.IDCity= d.IDCity

LEFT JOIN (SELECT * FROM media_mapping WHERE is_main_item=1 AND IDGalleryType=3) f
    ON d.IDGallery = f.IDGallery
left join course_tax
g on a.IDCourseTax = g.IDCourseTax


where a.IDCart = :cart_id
order by d.name, a.teetime_dt, b.start_time;";
    $prepared = array(
        "cart_id"   => $idCart,
    );
    $conn     = new DBConnection();
    $results  = $conn->fetch($query, $prepared);
    $conn     = null;

    $results = !empty($results) ? $results : array();


    $id = null;

    foreach($results as $row) {
        // Set ID for the first record.
        if($id === null)
            $id = $row['IDDestination'];



        // will stay true, otherwise it's false and we should kill the loop.
        if($id != $row['IDDestination']) {
            $newid=$row['IDDestination'];

           echo "<script type='text/javascript'> emptycart();</script>";
        $query = "DELETE FROM cart_course_teetimes a WHERE  h.IDDestination='.$id.'";
            $res =mysql_query($query) or die (mysql_error());

            break;
        }
    }

【问题讨论】:

    标签: php sql arrays join


    【解决方案1】:

    这是不正确的 PHP:

       $query = "DELETE FROM cart_course_teetimes a WHERE  h.IDDestination='.$id.'"
    

    你已经在一个"-quoted 字符串中,所以. PHP 连接运算符不是运算符,它们只是一个句点。

    您需要以下任何一种:

       $query = "DELETE FROM cart_course_teetimes a WHERE  h.IDDestination='" . $id . "'";
       $query = "DELETE FROM cart_course_teetimes a WHERE  h.IDDestination='$id'"
    

    现在你正在制作

     ... WHERE h.IDDestination = .42.
    

    这不是有效的 SQL。

    另外,您似乎正在混合数据库库。你有$conn-&gt;fetch() 这意味着你正在使用一个OO db库(mysqli?pdo?自定义包装器?)。但是您随后致电mysql_query()。除非您明确调用mysql_connect(),否则删除查询将永远不会执行。在其中一个库中建立的数据库连接在任何其他库中完全没有用。

    【讨论】:

    • 感谢您的帮助。我仍然收到一个致命错误,提示未捕获异常 'PDOException' 并带有消息'SQLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'a WHERE h.IDDestination='7'' 附近使用正确的语法...。这必须与 cart_course_teetimes 的 a 相关
    • 可以试试cart_course_teetimes AS a。我在 mysql 的文档中没有看到任何明确指出 as 在删除查询中是可选的。它在选择中是可选的,但删除文档中没有这样说。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2013-10-13
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    相关资源
    最近更新 更多