Click here to Skip to main content
16,017,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i have one table called city containg(id,name) and another table areas containg(id,name,cityid_pk).can anybody please help me for writing storeprocedure or any link regarding this .i am new in mysql database.i have to select areaes name through cityid as foreign key.

SQL
CREATE PROCEDURE Sp_Select_area
(
`name` VARCHAR(300)
)
BEGIN
SELECT areas.`name`
FROM areas
JOIN cities
ON areas.cityid_fk=cities.id
ORDER BY areas.`name`
END;


can anybody find out the error i have wriiten in mysql.
Posted
Updated 3-Nov-11 1:31am
v3

You need to use a JOIN[^] statement.
The SQL statement would be:
SQL
SELECT t1.name AS area, t2.name AS city
FROM areas AS t1
JOIN city AS t2 ON t2.id = t1.cityid_pk;
 
Share this answer
 
Hope it helpls
SQL
create procedure select_area()
begin 
	select
		areas.Name
	from   (( areas a JOIN city c 
                ON a.cityid_pk = c.id) JOIN table3 on a.cityid_pk = table3.id and c.id=table3.id) ;
	
end |

Learn MY SQL Stored Procedure
 
Share this answer
 
v2
Comments
chinta123 3-Nov-11 7:03am    
thank you very much ,if i have a table with 3 foreign key referencing 3 table,then i have to join 3 table and then select what i need???if it is correct can u pls mention how i join 3 tables??
uspatel 3-Nov-11 7:40am    
check updated answer.
RaviRanjanKr 4-Nov-11 4:09am    
useful answer, My 5+
uspatel 4-Nov-11 5:14am    
thanks......
You may refer below link for "MySQl Procedure".
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

And this link for "Joins".
http://dev.mysql.com/doc/refman/5.0/en/join.html
 
Share this answer
 
Comments
RaviRanjanKr 4-Nov-11 4:09am    
Nice Link, My 5+
RaisKazi 4-Nov-11 4:12am    
Thank you Ravi. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900