2013-02-03 104 views
-1

我的表由5個字段:SQL查詢與phpMyAdmin

Submission ValueId 1 2 3 4 5 
FormId 1 2 3 4 5 
SubmissionId 22 or 23 or 24 
FieldName fullname,Date,ville, 
FieldValue 1 Paul 01/10/2013 Bern 

表我想:

SELECT FormId, SubmissionId, FieldValue FROM qesnd_rsform_submission_values WHERE  FieldValue = Bern AND FormID='1' ORDER BY SubmissionId ASC" 

隨着formid:1
隨着fieldValue方法:保羅
隨着fieldValue方法:01/10/2013
With FielValue:Bern

我的公關如何使我的請求phpMyAdmin與多個選擇?

+1

難題?共享'show create table qesnd_rsform_submission_values'輸出。你是什​​麼意思多選?選擇多個記錄? – SparKot

+0

1.標準化您的數據 – Strawberry

回答

0

看起來您的數據有點過分規範化,每個字段都有單獨的行。我thnk最好的方式來訪問這個數據是通過聚合,假設字段名不重複任何formid:

select formid, submissionId, 
     max(case when fieldName = 'FullName' then fieldvalue end) as FullName, 
     max(case when fieldName = 'Date' then fieldvalue end) as DateStr, 
     max(case when fieldName = 'ville' then fieldValue end) as Ville 
from t 
where formid = 1 
group by SubmissionId 

如果你想選擇在過濾器上的信息,然後嘗試:

select * 
from (select formid, submissionId, 
      max(case when fieldName = 'FullName' then fieldvalue end) as FullName, 
      max(case when fieldName = 'Date' then fieldvalue end) as DateStr, 
      max(case when fieldName = 'ville' then fieldValue end) as Ville 
     from t 
     group by SubmissionId 
    ) t 
where formid = 1 and FullName = 'Paul' and DateStr = '01/10/2013' and Ville = 'Bern'