こんにちは。にいるです。
標準機能の勉強しないといけないんですが、あまりできていないですね。
もう一回計画を立てて試験範囲を総ナメしようと思います。
そして、今回はよく使うSOQLをメモがてらまとめていきたいと思います。
最近、実務でApex関係が増えてきたので。
1.一致検索(=,!=)
1 |
SELECT Id,Name FROM CONTACT WHERE NAME = '東京一郎' |
1 |
SELECT Id,Name FROM CONTACT WHERE NAME != '東京一郎' |
2.日付検索(=)
1 |
SELECT Id,Name, CloseDate FROM Opportunity WHERE CloseDate < TODAY |
※今日を含まない
1 |
" >SELECT Id,Name, CloseDate FROM Opportunity WHERE CloseDate <= TODAY |
※今日を含みます。
3.あいまい検索(LIKE)
1 |
SELECT Name FROM Contact WHERE Name LIKE '東京%' |
%はワイルドカードと呼び、部分一致検索に使用できます。
4.複数値検索(IN,NOT IN)
1 |
SELECT Name, Region__c FROM Account WHERE Region__c IN ('関西','四国') |
1 |
SELECT Name, Region__c FROM Account WHERE Region__c IN ('関西','四国') |
5.抽出件数の制御(LIMIT,OFFSET)
1 |
SELECT Id,Name, Phone FROM Account LIMIT 5 |
1 |
SELECT Id,Name, Phone FROM Account OFFSET 5 |
6.集計関数(COUNT,COUNT_DISTINCT,SUM,AVG,MIN,MAX,GROUP BY)
1 |
SELECT COUNT(ID) FROM Opportunity WHERE StageName = 'Closed Won' |
1 |
SELECT COUNT_DISTINCT(Name) FROM Opportunity WHERE StageName = 'Closed Won' |
1 |
SELECT MAX(Amount), CloseDate FROM Opportunity WHERE StageName = 'Closed Won' GROUP BY CloseDate |
※集計関数を使用する場合は、GROUP BY句の記載も必要になります。
1 |
SELECT MIN(Amount), CloseDate FROM Opportunity WHERE StageName = 'Closed Won' GROUP BY CloseDate |
1 |
SELECT SUM(Amount), CloseDate FROM Opportunity WHERE StageName = 'Closed Won' GROUP BY CloseDate |
1 |
SELECT AVG(Amount), CloseDate FROM Opportunity WHERE StageName = 'Closed Won' GROUP BY CloseDate |
7.結果の並び替え(ORDER BY)
1 |
SELECT Id,Name,CloseDate FROM Opportunity ORDER BY CloseDate ASC |
並び替えの順序はASCで指定して商談完了日が古いものから表示されます
1 |
SELECT Id,Name,CloseDate FROM Opportunity ORDER BY CloseDate DESC |
1 |
SELECT Id,Name,CloseDate FROM Opportunity ORDER BY CloseDate NULLS FIRST |
1 |
SELECT Id,Name,CloseDate FROM Opportunity ORDER BY CloseDate NULLS LAST |