/    Sign up×
Community /Pin to ProfileBookmark

Here is a query that is giving me an error

[code]
SELECT a.wine_num, a.activity, date_format(a.date_posted,’%m-%d-%y’) as date_mdy, d.activity_desc, a.bottle_cnt, a.my_comments, w.label, w.vintage, CONCAT(s.sort_date, s.wine_num) as sort_col

FROM mywines_activity a, mywines_activity_codes d, mywines w

LEFT OUTER JOIN (select wine_num, max(date_posted) as sort_date
from mywines_activity
group by wine_num) s
on a.wine_num = s.wine_num

WHERE $sel_wine
a.activity = d.activity_code and
a.wine_num = w.wine_num
ORDER BY sort_date Desc, a.wine_num

[/code]

The message is that ‘a.wine_num’ in the ON clause of my join does not exist. The query works fine without the join, so I can’t figure this out.
Here’s the actual message:
Fatal error: Uncaught PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘a.wine_num’ in ‘on clause’ in /home/albany/public_html/homejg/wine_activity_review.php:103 Stack trace: #0 /home/albany/public_html/homejg/wine_activity_review.php(103): PDO->prepare(‘SELECT a.wine_n…’) #1 {main} thrown in /home/albany/public_html/homejg/wine_activity_review.php on line 103

Any ideas?

to post a comment

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 30.2021 — Nothing obviously jumps out at me other than...are you positive that mywines_activity.wine_num is a thing? (It _might_ be that the on clause is the first place it actually looks for that column definition when parsing the query.)

Assuming that's not the issue, I'm curious if there's something different in the way MySQL handles the implicit joins in the FROM clause. Pending someone pointing out something I'm not seeing, I might try splitting that up into 3 lines: a FROM and two INNER JOIN lines. 🤷

I guess in other words: nope, I don't know. 😉
Copy linkTweet thisAlerts:
@ginerjmauthorSep 30.2021 — Yes - it is the key of that table. As I said the whole thing (w/out the join ) runs fine. I'm just trying to improve the sort sequence by getting the latest date from each wine_num's records with the JOIN in order to sort multiple wines in order by the most recently active wine_num

Looked at all of my notes and samples of previous joins and am stymied by this one.

PS - and if you look closely you can see that I use the a.wine_num in 2 other places in the query. Of course they are mentioned later so I'm just seeing the first error message I guess.
Copy linkTweet thisAlerts:
@ginerjmauthorSep 30.2021 — Here's something to ponder:

If I add an incorrect column name right at the beginning of my first line the query processor finds fault with it

The first line looks like
$q = "SELECT a.wine_num, a.xxx, a.activity, date_format(a.date_posted,'%m-%d-%y')
and I get the error message that a.xxx is not a column name, but not the message about a.wine_num which precedes it. Therefore a.wine_num is NOT an invalid name?
Copy linkTweet thisAlerts:
@NogDogSep 30.2021 — Okay, just spit-balling things here, but wondering if that max date could just be put into the select itself?
[code=sql]
SELECT
a.wine_num,
a.activity,
date_format(a.date_posted,'%m-%d-%y') as date_mdy,
d.activity_desc,
a.bottle_cnt,
a.my_comments,
w.label,
w.vintage,
CONCAT(
(
select max(date_posted)
from mywines_activity
where wine_num = a.wine_num
)
) as sort_col
FROM mywines_activity a
INNER JOIN mywines_activity_codes d on a.activity = d.activity_code
INNER JOIN mywines w on a.wine_num = w.wine_num
WHERE $sel_wine
ORDER BY sort_date Desc, a.wine_num
[/code]
Copy linkTweet thisAlerts:
@NogDogOct 01.2021 — PS: Do you really want/need to concat that date_posted with the wine_num? (Just seems odd to me since the result row will already have the wine_num.)
×

Success!

Help @ginerjm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...