I am using MySQL and I am having the following scenario:
Table nodes: node_id, lat, lng, name
Table links: node1, node2, name
So there are two tables, in table nodes, it stores all the point and their respective latitude and longitude, and in the table link, where store node1 which reference nodes, and node2, which reference nodes too.
Since in MySQL and Rails we can’t really have 2 foreign key pointing to the same table (correct me if I am wrong) and for example if I want to find the starting node_name and ending node_name, how would I construct my SQL statement? I tried
SELECT nodes.name from Nodes, links WHERE nodes.node_id = node1 which kinda works but very slow (I have less than 10k records in each table), and if I want to find both names for starting node and ending node, how can I go over and do it? Or if I want to limit the starting node with lat > x and ending node with lat < y to find all the links?
Thank you.
Regards,
Andy.
yes you can do this. your table structure looks good.
your query is also good. try making sure you have proper indexes on the node_id to help performance.
you can run 2 queries, one for each name, or you can do a union r two subqueries if you want the results to all be in the same query.