簡體   English   中英

PHP SQL查詢高級搜索

[英]php sql query for advanced search

我需要對高級搜索進行特定的查詢。

我提供了一個數據庫結構示例

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

現在我必須在此表中進行一些搜索並獲取正確的id_post,例如:

我希望所有具有代碼= reg和content = typeY的id_post在這種情況下都沒有結果

2d示例->所有id_post的代碼= reg和content = lazio的結果必須為1和2

3d示例->所有id_post具有(code = reg and content = lazio)和(code = type and content = typeY)的結果必須為2

等等....

如何設置三個查詢?

嘗試這個:

范例1:

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

范例2:

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

范例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'

我已經運行測試以驗證結果。 您可以在此sqlfiddle上自己驗證它們

示例3基本上是一個交集 ,這就是為什么andor方法都不起作用。 您可以參考此問題以獲取解決此類查詢的其他方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM