0

I have this query in mysql.

SELECT
    a.*, b.material_tools_group
FROM
    borrowing_mat_master_data AS a
INNER JOIN material_tools_master_data AS b ON a.tools_code = b.material_code
WHERE
    material_tools_group IN (
        'Consumable',
        'Material'
    )
ORDER BY
    `a`.`tools_code` ASC

I want to delete the result from the query above, I've tried this query below but it doesn't work.

DELETE
FROM
    borrowing_mat_master_data
WHERE
    tools_code IN (
        SELECT
            a.*, b.material_tools_group
        FROM
            borrowing_mat_master_data AS a
        INNER JOIN material_tools_master_data AS b ON a.tools_code = b.material_code
        WHERE
            material_tools_group IN (
                'Consumable',
                'Material'
            )
    );

Any help will be much appreciated.

Regards.

1000111
  • 12,709
  • 2
  • 24
  • 33
M Ansyori
  • 387
  • 4
  • 18

2 Answers2

0

In order to delete data from borrowing_mat_master_data table out of the joined result:

DELETE a
FROM
    borrowing_mat_master_data AS a
INNER JOIN material_tools_master_data AS b ON a.tools_code = b.material_code
WHERE
    material_tools_group IN (
        'Consumable',
        'Material'
    );

In order to delete data both from borrowing_mat_master_data and material_tools_master_data table out of the joined result:

DELETE a,b
FROM
    borrowing_mat_master_data AS a
INNER JOIN material_tools_master_data AS b ON a.tools_code = b.material_code
WHERE
    material_tools_group IN (
        'Consumable',
        'Material'
    )

See Delete with join (single table, multiple tables)

Community
  • 1
  • 1
1000111
  • 12,709
  • 2
  • 24
  • 33
0

If you want to delete whole records of form table name borrowing_mat_master_data and only material_tools_group column from table name material_tools_master_data use following query i hope it will help you.

DELETE a FROM borrowing_mat_master_data AS a
INNER JOIN material_tools_master_data AS b ON a.tools_code = b.material_code
WHERE
    b.material_tools_group IN (
        'Consumable',
        'Material'
    )
Rana Aalamgeer
  • 692
  • 2
  • 7
  • 22