I have a pain finding the correct MySQL statement.
Records are like
id leadid fieldnumber value
1 1 100 AAA
2 1 101 yes
3 1 102 email1
4 2 100 AAA
5 2 101 no
6 2 102 email2
7 3 100 BBB
8 3 101 yes
9 3 102 email3
10 4 100 AAA
11 4 101 yes
12 4 102 email4
Now I want to collect from every unique leadid the value column from the record with fieldnumber = 102 where the corresponding records with the same leadid satisfy:
(fieldnumber = 100 AND value = "AAA") AND
(fieldnumber = 101 AND value = "yes")
In this example this results in: email1, email4
I got already result with the first selection statement ( fieldnumber = 100 AND value = "AAA"
) but when I add the second one ( fieldnumber = 101 AND value = "yes"
), the result is always zero records.
I suppose it's a stupid thing, but I can't figure out or find the correct statement to do so.
The key point here is to recognize that you are working with three records from the same table at the same time. You have to join the table with itself, twice — these are called self-joins .
You need something like:
SELECT a3.value
FROM Anonymous_Table AS a1
JOIN Anonymous_Table AS a2 ON a1.leadid = a2.leadid
JOIN Anonymous_Table AS a3 ON a1.leadid = a3.leadid
WHERE (a1.fieldnumber = 100 AND a1.value = "AAA")
AND (a2.fieldnumber = 101 AND a2.value = "yes")
AND (a3.fieldnumber = 102)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.