我在互联网上搜索过,但我找不到问题的答案也没有运气。我希望这还没有特别发布,但如果你能指出我能找到答案的地方,我将不胜感激。
我能够得到以下表格数据:
id lead_id form_id field_number value
1 1 1 1 Hosker
2 1 1 7 b**********@yahoo.com
3 1 1 6 Hyundai Tournament of Champions
4 1 1 3 Adam Scott
5 1 1 4 Harris English
6 1 1 5 2014-01-02 23:59:47
7 1 1 8 5b409692-e9ed-486e-8d77-7d734f1e023d
使用此查询字符串获取下面的结果:
SELECT id, lead_id, form_id,
MAX(case when field_number = 1 then value end) username,
MAX(case when field_number = 7 then value end) email,
MAX(case when field_number = 6 then value end) tournament_name,
MAX(case when field_number = 3 then value end) primary_golfer,
MAX(case when field_number = 4 then value end) backup_golfer,
MAX(case when field_number = 5 then value end) date,
MAX(case when field_number = 8 then value end) tournament_id
FROM `wp_rg_lead_detail` GROUP BY lead_id
id lead_id form_id field_number value value value value value value value
1 1 1 1 Hosker b**********@yahoo.com Hyundai Tournament of Champions Adam Scott Harris English 2014-01-02 23:59:47 5b409692-e9ed-486e-8d77-7d734f1e023d
我只希望该查询字符串仅在tournament_id列的值等于特定值时才提取相关数据。我该怎么做?
答案 0 :(得分:0)
如果我理解你的要求,请尝试:
SELECT * FROM TABLE_NAME
WHERE tournament_id = specific_value;
答案 1 :(得分:0)
尝试此查询。我想你可以使用HAVING
子句。
SELECT id, lead_id, form_id,
MAX(case when field_number = 1 then value end) username,
MAX(case when field_number = 7 then value end) email,
MAX(case when field_number = 6 then value end) tournament_name,
MAX(case when field_number = 3 then value end) primary_golfer,
MAX(case when field_number = 4 then value end) backup_golfer,
MAX(case when field_number = 5 then value end) date,
MAX(case when field_number = 8 then value end) tournament_id
FROM `wp_rg_lead_detail` GROUP BY lead_id
HAVING tournament_id = '5b409692-e9ed-486e-8d77-7d734f1e023d'
sqlFiddle在sqlFiddle中我有2个不同的tournament_id的样本数据,并且应用HAVING
子句以便只返回该tournament_id的值,你可以尝试删除HAVING并看到它返回2行。
您的Group By不是标准的,因为您也选择了id和form_id。我建议你删除id或使用MIN(id)或MAX(id)和GROUP BY lead_id,form_id