简体   繁体   English

PHP SQL查询高级搜索

[英]php sql query for advanced search

i need to do a particular query for an advanced search. 我需要对高级搜索进行特定的查询。

i provide a sample of db structure 我提供了一个数据库结构示例

id         code        content        id_post
--------   --------    -----------    -------------
1          reg         lazio          1
2          reg         lazio          2
3          type        typeX          1
4          type        typeY          2

now i have to do some search in this table and get right id_post, for example: 现在我必须在此表中进行一些搜索并获取正确的id_post,例如:

i want all id_post that have code = reg and content = typeY in this case there are no results 我希望所有具有代码= reg和content = typeY的id_post在这种情况下都没有结果

2d example--> all id_post with code = reg and content = lazio the result must be 1 and 2 2d示例->所有id_post的代码= reg和content = lazio的结果必须为1和2

3d example--> all id_post with (code = reg and content = lazio) and (code = type and content = typeY) the result must be 2 3d示例->所有id_post具有(code = reg and content = lazio)和(code = type and content = typeY)的结果必须为2

and so on.... 等等....

how can I set the three queries? 如何设置三个查询?

Try this: 尝试这个:

Example 1: 范例1:

select id_post from your_table where code = 'reg' and content = 'typeY'

Example 2: 范例2:

select id_post from your_table where code = 'reg' and content = 'lazio'

Example 3: 范例3:

select t1.id_post from your_table as t1
inner join (select id_post from your_table where code = 'type' and content = 'typeY') as t2 on t1.id_post = t2.id_post
where t1.code = 'reg' and t1.content = 'lazio'

I have run tests to verify the results. 我已经运行测试以验证结果。 You can verify them yourself on this sqlfiddle 您可以在此sqlfiddle上自己验证它们

Example 3 is basically an intersection , which is why neither the and and or approaches work. 示例3基本上是一个交集 ,这就是为什么andor方法都不起作用。 You can refer to this question for further ways to solve this type of query. 您可以参考此问题以获取解决此类查询的其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM