MikeMikeMike Posted May 14, 2011 Share Posted May 14, 2011 Around page 197, Larry starts explaining some of the SQL statements that will be used in the stored procedures, but there's one thing I don't get. You see how he uses the short forms "gc" and "s" and "sc" throughout? SELECT gc.description, gc.image, CONCAT("C", sc.id) AS sku, CONCAT_WS(" - ", s.size, sc.caf_decaf, sc.ground_whole, sc.price) AS name, sc.stock FROM specific_coffees AS sc INNER JOIN sizes AS s ON s.id=sc.size_id INNER JOIN general_coffees AS gc ON gc.id=sc.general_coffee_id WHERE general_coffee_id=cat AND stock>0 I understand that "gc" refers to general coffees, but I don't understand how the database understands this query because when the tables were created, there were no short forms used. For example, the table for general coffees was created thus CREATE TABLE `general_coffees` ( `id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT, `category` varchar(40) NOT NULL, `description` tinytext, `image` varchar(45) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `type` (`category`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; so I would expect a query that uses "gc.description" to fail because how does it understand the "gc"? thanks if you can help. Link to comment Share on other sites More sharing options...
Jonathon Posted May 14, 2011 Share Posted May 14, 2011 Hi MikeMikeMike Take note of this line FROM specific_coffees AS sc And INNER JOIN general_coffees AS gc This is where the shortcut so to speak is created so the database recognises 'gc' as the table 'general coffees'. 1 Link to comment Share on other sites More sharing options...
MikeMikeMike Posted May 14, 2011 Author Share Posted May 14, 2011 So you can designate the shortcut (in the third line) after it's already been used (in the first line)? Hi MikeMikeMike Take note of this line FROM specific_coffees AS sc And INNER JOIN general_coffees AS gc This is where the shortcut so to speak is created so the database recognises 'gc' as the table 'general coffees'. Link to comment Share on other sites More sharing options...
Jonathon Posted May 14, 2011 Share Posted May 14, 2011 Yes!!Mad isn't it! I think 1 of my very first questions on larry's site, back when it was called something else was about this. It's a weird thing to get your head around isn't it. 1 Link to comment Share on other sites More sharing options...
Heyitsrick Posted May 14, 2011 Share Posted May 14, 2011 I am no expert (newbie!), trust me, but I would guess that the SELECT portion of the query using the shortcut/aliases doesn't really come into play until all the other parameters/conditions are meted out (FROM, INNER JOIN, WHERE, etc.) Once all the conditions of the query are set in place, then it will go about trying to "understand" the elements being selected. That's my uneducated guess, and maybe someone else will chime in to verify or dispute that. 1 Link to comment Share on other sites More sharing options...
Larry Posted May 16, 2011 Share Posted May 16, 2011 Yes, that's exactly correct, Rick. Thanks and kudos! MySQL executes queries through a series of steps, not in sequential order through the query. Link to comment Share on other sites More sharing options...
Recommended Posts