I have the following sql statement:
SELECT
COUNT(table2.programName),
table2.programName
FROM
table1
LEFT JOIN
table2
ON
table1.programID = table2.programID
WHERE
table1.MemberID = 12345
AND
table2.programName = (table2.programName associated with dynamic table2.programID)
I do realize the last AND statement is currently invalid but it is what I cannot seem to get my head around. While I can execute the query successfully by inserting a known value, the query needs to be generated dynamically (done with php) and the value will appear as a variable. I only put in parenthesis to show what I am trying to accomplish.
Any ideas how would I go about finding the programName associated with the programID in the parenthesis in a single sql statement?
EDIT – POSSIBLE ANSWER
So in testing out some more, I think I may have found the answer but am unsure if it is ‘good’ practice. Here is my code:
SELECT
COUNT(table2.programName),
table2.programName
FROM
table1
LEFT JOIN
table2
ON
table1.programID = table2.programID
WHERE
table1.MemberID = 12345
AND
table2.programName = (SELECT table2.programName FROM table2 WHERE table2.programID = $id);
Use: