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
  • 13,169
  • 2
  • 28
  • 37
M Ansyori
  • 429
  • 6
  • 21

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
  • 13,169
  • 2
  • 28
  • 37
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
  • 702
  • 2
  • 8
  • 22