Jump to content

Need help understanding SQL Query.


atrum

Recommended Posts

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 . ";

Link to comment
Share on other sites

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 . ";

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.