So this is focused on pulling the "pdf_url" value from the presentation table. I want to pull the newest "pdf_url" for each company, so I am using the MAX() function.
I am using Soda companies as an example. Pepsi, has three urls and I want to pull the newest pdf_url that is not NULL. Coke has one row, but is missing a pdf_url, but my SQL statement is pulling that row as empty. RC Cola, is working fine.
Here is my SQL:
SELECT pdf_url FROM presentation WHERE id = (SELECT MAX(id) FROM presentation WHERE exec_url IS NOT NULL AND id=$value)
Here is my table structure:
company_id | name
1 | Pepsi
2 | Coke
3 | RC Cola
id | company_id | pdf_url | presentation_url
1 | 1 | file1.pdf | pres1.html
2 | 2 | NULL | pres2.html
3 | 1 | file2.pdf | pres3.html
4 | 3 | file3.pdf | pres4.html
5 | 1 | NULL | pres5.html
With the above SQL I am incorrectly getting these row id's from the presentation table:
2, 4, 5
I want to get the rows from the presentation table:
In my SQL it seems like I am not properly filtering out the NULL values, which is causing the issues. Its like I want to "Give me all the rows from the presentation table where pdf_url is not null, then with those results, give me the MAX(id) for each company_id".