atrum Posted June 5, 2011 Share Posted June 5, 2011 Hello everyone, I am working on a project that uses an sql query that is a little more advanced than what I have been exposed to in the past. some I understand most of it but it has some parts I don't really grasp. Could any one break this down for me into parts and explain what it is this is doing? SELECT c.* , (SELECT COUNT( * ) FROM `referrals` AS r WHERE r.`referrals_contact_id` = c.`contacts_id` ) AS gl_count FROM `contacts` AS c WHERE c.`contacts_demo_date` >= '" . $fromDateDB . "' AND c.`contacts_demo_date` <= '" . $toDateDB . "' AND `" . $_REQUEST['queryColumn'] . "` = " . $answer . "; Quote Link to comment https://forums.phpfreaks.com/topic/238438-need-help-understanding-sql-query/ Share on other sites More sharing options...
teynon Posted June 5, 2011 Share Posted June 5, 2011 SELECT c.* , // Select all columns from contacts // Count number of values in referrals (SELECT COUNT( * ) FROM `referrals` AS r WHERE r.`referrals_contact_id` = c.`contacts_id` ) AS gl_count // refer to table "contacts" as c FROM `contacts` AS c // Conditions for results for c. WHERE c.`contacts_demo_date` >= '" . $fromDateDB . "' AND c.`contacts_demo_date` <= '" . $toDateDB . "' AND `" . $_REQUEST['queryColumn'] . "` = " . $answer . "; Quote Link to comment https://forums.phpfreaks.com/topic/238438-need-help-understanding-sql-query/#findComment-1225319 Share on other sites More sharing options...
atrum Posted June 5, 2011 Author Share Posted June 5, 2011 Thanks for the reply teynon. On that first part that says SELECT c.* how is it that it knows that I want all columns from contacts when it seems c is assigned later in the query. Or like php does the order not matter? Quote Link to comment https://forums.phpfreaks.com/topic/238438-need-help-understanding-sql-query/#findComment-1225323 Share on other sites More sharing options...
teynon Posted June 5, 2011 Share Posted June 5, 2011 It doesnt matter. How else could you define specific tables in the results? * = wildcard / all c.* = table referenced as c . all columns Quote Link to comment https://forums.phpfreaks.com/topic/238438-need-help-understanding-sql-query/#findComment-1225325 Share on other sites More sharing options...
atrum Posted June 5, 2011 Author Share Posted June 5, 2011 Thanks man, appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/238438-need-help-understanding-sql-query/#findComment-1225356 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.