Mysql 根据表B从表A中获取数据
我需要一些帮助 表A(操作) 表B(操作元数据) 用户可以在下拉列表中选择:Mysql 根据表B从表A中获取数据,mysql,sql,Mysql,Sql,我需要一些帮助 表A(操作) 表B(操作元数据) 用户可以在下拉列表中选择: 元数据字段1作为多选选项,显示第一个条目 “空的” 元数据字段2作为多选选项,第一个条目显示为“空” 用户希望获得符合多选选项的所有操作 我的想法如下: 我需要从表A中获取所有行 其中表B数据_字段_id_1位于(某些id) 或(某些id)中的表B数据字段id 2 或者表a\U id不在表B中 或数据_字段_id_1为空 或数据_字段_id_2为空 或数据_字段_id_3为空 有人能帮我处理这个SQL查询吗?
- 元数据字段1作为多选选项,显示第一个条目 “空的”
- 元数据字段2作为多选选项,第一个条目显示为“空”
- 其中表B数据_字段_id_1位于(某些id)
- 或(某些id)中的表B数据字段id 2
- 或者表a\U id不在表B中
- 或数据_字段_id_1为空
- 或数据_字段_id_2为空
- 或数据_字段_id_3为空
选择一个*
select a.*
from tablea a join tableb b
on a.table_a_id <> b.table_a_id
where b.data_field_id_1 IN (1,2,3) --replacement needed
or b.data_field_id_2 IN (4,5,6) --replacement needed
-- or a.table_a_id is NOT IN Table B
or b.data_field_id_1 IS NULL
or b.data_field_id_2 IS NULL
or b.data_field_id_3 IS NULL
从表a连接表b
在a.table_a_id b.table_a_id上
其中(1,2,3)中的b.data_字段_id_1--需要更换
或(4,5,6)中的b.data_字段_id_2--需要更换
--或者a.表a\U id不在表B中
或b.data\u字段\u id\u 1为空
或b.data_字段_id_2为空
或b.data_字段_id_3为空
这应该让你开始。这些表是根据您的条件之一加入的
表a\u id不在表B
我会尝试使用左外部联接
来向您显示tablea在tableb中没有子级的行
SELECT a.table_a_id,
a.data
FROM TABLEA a
LEFT OUTER JOIN TABLEB b ON a.table_a_id = b.table_a_id
WHERE b.data_field_id_1 IN (your ids)
OR b.data_field_id_2 IN (your other ids)
OR b.data_field_id_1 IS NULL
OR b.data_field_id_2 IS NULL
OR b.data_field_id_3 IS NULL
您可以进行连接,但我喜欢使用LEFT JOIN并排查看数据:-)您是否尝试过将sql语句中明确提到的所有条件组合在一起?示例数据和预期结果将有帮助,谢谢!我使用左连接,尽管我想先在没有连接的情况下完成它。。。但它看起来很快,我需要的都能做到
select a.*
from tablea a join tableb b
on a.table_a_id <> b.table_a_id
where b.data_field_id_1 IN (1,2,3) --replacement needed
or b.data_field_id_2 IN (4,5,6) --replacement needed
-- or a.table_a_id is NOT IN Table B
or b.data_field_id_1 IS NULL
or b.data_field_id_2 IS NULL
or b.data_field_id_3 IS NULL
SELECT a.table_a_id,
a.data
FROM TABLEA a
LEFT OUTER JOIN TABLEB b ON a.table_a_id = b.table_a_id
WHERE b.data_field_id_1 IN (your ids)
OR b.data_field_id_2 IN (your other ids)
OR b.data_field_id_1 IS NULL
OR b.data_field_id_2 IS NULL
OR b.data_field_id_3 IS NULL
SELECT T1.*, T2.* FROM table_a
LEFT JOIN table_b
ON T1.table_a_id = T2.table_a_id
where T2.data_field_id_1 IN (--values)
OR T2.data_field_id_2 IN (--values)
OR T1.table_a_id is NOT IN table_b
OR T2.data_field_id_1 IS NULL
OR T2.data_field_id_2 IS NULL
OR T2.data_field_id_3 IS NULL