简体   繁体   English

如何正确匹配左联接?

[英]How to properly match the left join?

I'm trying to properly match a left-join, where you delete filter and information pieces that are recorded in X table for this filter. 我正在尝试正确匹配左联接,在该联接中您将删除该X过滤器中记录的filter和信息。

Here is my example: 这是我的示例:

table d_category 表d_category

filter_id
7
8

table d_name 表d_name

id | filter_id
10 | 7
11 | 7
12 | 7

table product_d 表product_d

product_id | d_name_id
50         | 10
50         | 11
50         | 12

The Query 查询

$this->db->query("DELETE FROM product_d
LEFT JOIN d_name ON (product_d.d_name_id = d_name.id) 
WHERE"); // what should this clause be?

I assume that you are looking for Cascading Delete . 我假设您正在寻找级联删除 If that is the case: 如果是这样的话:

If you configure your Foreign Keys to have cascade delete , all related data will also get deleted when you delete record from parent table. 如果将外键配置为具有cascade delete ,则从父表中删除记录时,所有相关数据也将被删除。

CREATE TABLE rooms (
    room_no INT PRIMARY KEY AUTO_INCREMENT,
    room_name VARCHAR(255) NOT NULL,
    building_no INT NOT NULL,
    FOREIGN KEY (building_no)
        REFERENCES buildings (building_no)
        ON DELETE CASCADE
);

then when you delete building, rooms also get deleted 那么当您删除建筑物时,房间也会被删除

DELETE FROM buildings 
WHERE
  building_no = 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM