我在搜索子表时遇到问题。这是一个简单的例子:
table:project_details
我想通过名称,描述搜索项目,然后将其与project_list_id相关联,例如zip和state。
我知道问题在于我如何搜索详细的表格。所以我只用一张桌子把它简化为更简单的东西:
SELECT `ProjectDetail`.`project_list_id` FROM `database`.`project_details` AS `ProjectDetail` WHERE (
(
(
(`ProjectDetail`.`answer` = '96720')
AND
(`ProjectDetail`.`field_name` = 'zip')
)
)
AND (
(
(`ProjectDetail`.`answer` = 'HI')
AND
(`ProjectDetail`.`field_name` = 'state')
)
)
)
我想选择满足这两个字段要求的project_list_id,但由于它们是唯一的行,因此不会返回任何内容。
答案 0 :(得分:1)
根据我的理解,你不想返回所有的items_ids,它们分别为每个id满足两个条件。所以这应该有效:
SELECT a.project_list_id
FROM database.project_details a
INNER JOIN database.project_details b
ON a.project_list_id = b.project_list_id
WHERE a.answer = '96720' AND a.field_name = 'zip'
AND b.answer = 'HI' AND b.field_name = 'state'