Reputation: 203
I am creating a procedure where I'm supposed to look for the last date of an exam.
So I created a local variable for this because I will need this date in other things as well. However, when I do a simple SELECT of this local variable, I get the message:
Unknown column 'last_exam' in 'field list'.
Code:
DECLARE latest_exam date;
SELECT DATE(MAX(ex_date))
INTO latest_exam
FROM vets
WHERE an_id = p_animal_id
GROUP BY an_id;
SELECT latest_exam, and a bunch of other stuff. ;
In the select, do I need to include a FROM clause? I didn't think so because I figured latest_exam is within the procedure.
Upvotes: 2
Views: 3828
Reputation: 1391
I had the same problem; it turned out not to be my SELECT INTO query within the procedure (which, yes, is valid in MySQL), but a call made later on in that outer procedure to another procedure with that actual error.
I only found this after confirming my query outside of the procedure with mock values:
SET @this_RID=0, @this_RANo=0, @this_modpos=0;
SELECT Response_ID, Response_attempt_No, position, UNCOMPRESS(RAMX.`data`)
INTO @this_RID, @this_RANo, @this_modpos, @this_XML
FROM RAMX
WHERE
Response_ID>=@this_RID AND Response_attempt_No>=@this_RANo AND position>=@this_modpos
AND (NOT (Response_ID=@this_RID AND Response_attempt_No=@this_RANo AND position=@this_modpos))
AND module_ID=2
AND `data` <> ""
ORDER BY Response_ID, Response_attempt_No, position
LIMIT 1;
SELECT @this_RID, @this_RANo, @this_modpos;
+-----------+------------+--------------+
| @this_RID | @this_RANo | @this_modpos |
+-----------+------------+--------------+
| 451994 | 0 | 1 |
+-----------+------------+--------------+
1 row in set (0.00 sec)
Calling the outer procedure was giving this error before correcting the procedure it called:
ERROR 1054 (42S22): Unknown column 'data' in 'field list'
Upvotes: 0
Reputation: 263723
I think CROSS JOIN
can answer your question since it produces cartesian product from both tables. Try this.
SELECT x.maxDate,
b.*
FROM tableName b,
(
SELECT DATE (max(ex_date)) maxDate
FROM vets
WHERE an_id = p_animal_id
GROUP BY an_id
) x
Upvotes: 1
Reputation: 12672
you're not assigning the result of your select in the variable.
Do this
select latest_exam = date(max(ex_date))..
Upvotes: 1