我有两个表,assets 和 asset_params。
Assets
|asset_id| some_asset_data |
-----------------------------
| 1 | 'some data' |
| 2 | 'some other data'|
Assets 参数
|asset_param_id|asset_id|param_name|param_value|
------------------------------------------------
| 1 | 1 | 'Memory' | '4096' |
| 2 | 1 | 'OS' | 'Windows' |
| 3 | 2 | 'Memory' | '4096' |
| 4 | 2 | 'OS' | 'Linux' |
现在,我如何找到具有参数的 Assets ,其中它们有一个名为“Memory”且值为“4096”的参数和一个名为“OS”且值为“Windows”的参数。
期望的结果是在示例中我找到一个 Assets 行,id 为 1。
我找不到合理的解决方案。感谢您的帮助。
最佳答案
select * from assets a where
exists (select 1 from asset_params p
where name = "OS" and value = "Win" and a.asset_id=p.asset_id)
and exists (select 1 from asset_params p
where name = "memory" and value = "4096" and a.asset_id=p.asset_id)
关于sql检查连接表是否有多个条件的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34964460/