我正在尝试找到正确的语法来删除不在逗号分隔行中的记录。
table A
| id | product_id | attribute_id |
|----|------------|--------------|
| 1 | 123 | 45 |
| 2 | 123 | 46 |
| 3 | 124 | 34 |
| 4 | 124 | 33 |
table B
| code | Axis |
|------|-------|
| 123 | 45,46 |
| 124 | 34 |
我的目标是从表 A 中删除属性 id 不在表 B 轴值中的所有行(在本例中为 id = 4 的行)。
我尝试做一个SELECT
before:
SELECT A.attribute_id, A.product_id
FROM tableA as A
LEFT JOIN (SELECT * FROM tableB) AS B
ON FIND_IN_SET(A.attribute_id, B.`axis`)
但没有任何运气。
我怎样才能做到这一点?
您可以尝试以下操作SELECT
数据:
SELECT A.attribute_id, A.product_id
FROM tableA AS A LEFT JOIN tableB AS B ON A.product_id = B.code
WHERE IFNULL(FIND_IN_SET(A.attribute_id, B.Axis), 0) = 0
您可以使用以下内容DELETE
表 A 上的行(基于SELECT
):
DELETE FROM tableA WHERE id IN (
SELECT A.id
FROM tableA AS A LEFT JOIN tableB AS B ON A.product_id = B.code
WHERE IFNULL(FIND_IN_SET(A.attribute_id, B.Axis), 0) = 0
)
demo: http://sqlfiddle.com/#!9/3a3e13/6/0 http://sqlfiddle.com/#!9/3a3e13/6/0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)