WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2007, 10:11 AM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Possible MySQL Doozie...

Hi every/anyone.

I was wondering if anyone could help me with a MySQL problem that I'm trying to figure out.

I'm working with 5 tables. The Sales, , Salesdetail, Product, Contacts and Users tables.

In the users table there is a field labelled referer. It is the referer the user came from (ie google search) that lead to the sale.

Generally I can fetch the referer from the users table with

users.contact_id = contacts.contact_id

But what I am trying to get is a list of referers that lead to a sale with a specific product number (products table)...

Below is the basics of each table.



Code:
 
mysql> describe product;
+------------------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+----------------------+------+-----+---------+-------+
| product_no | smallint(5) unsigned | | PRI | 0 | |
| 
+------------------+----------------------+------+-----+---------+-------+
 
----
 
mysql> describe users;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| user_id | int(10) unsigned | | PRI | NULL | auto_increment |
| referer | varchar(255) | YES | | NULL |
| contact_id | int(10) unsigned | | | 0 | |
+------------+------------------+------+-----+---------+----------------+
 
----
 
mysql> describe sales;
+--------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------------+------+-----+---------+----------------+
| sales_id | int(10) unsigned | | PRI | NULL | auto_increment |
| contact_id | int(10) unsigned | | MUL | 0 | |
| sale_date | datetime | YES | | NULL | |
+--------------------+---------------------+------+-----+---------+----------------+
 
----
 
mysql> describe salesdetail
-> ;
+----------------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+----------------------+------+-----+---------+----------------+
| salesdetail_id | int(10) unsigned | | PRI | NULL | auto_increment |
| sales_id | int(10) unsigned | YES | MUL | NULL | |
| product_no | smallint(5) unsigned | YES | MUL | NULL | |
| size | varchar(50) | | | | |
| time_updated | datetime | YES | | NULL | |
+----------------+----------------------+------+-----+---------+----------------+
 
----
 
mysql> describe contacts;
+--------------+---------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------------------------+------+-----+---------+----------------+
| contact_id | int(10) unsigned | | PRI | NULL | auto_increment |
| first_name | varchar(50) | YES | | NULL | |
| last_name | varchar(50) | YES | | NULL | |
| address | varchar(80) | YES | | NULL | |
+--------------+---------------------------------------+------+-----+---------+----------------+
 
---
So basically it's pretty easy to get the one referer from a product that was sold but is it possible to get all the referers from one product and all its sales?

I hope I haven't confused anyone. I'm not the greatest with MySQL but I'm getting there...Any help is appreciated.

Thanks everyone.

Last edited by jtracking : 06-25-2007 at 10:47 AM. Reason: Found another table
Reply With Quote
  #2 (permalink)  
Old 06-25-2007, 12:21 PM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Re: Possible MySQL Doozie...

OK So I've tried many SQL statements to no avail but the one below 'seems' to be the closest to what I need but I'm still getting an error...

Code:
select referer, contact_id   
from users where users.contact_id in
(select sales_id, contact_id from sales, salesdetail where
sales.sales_id = salesdetail.sales_id
and salesdetail.product_no = #pnumber#);
The error states: Column 'sales_id' in field list is ambiguous

Bare with me and if you have any idears, feel free to let me know them...

P.S. #pnumber# would be product number.


Thanks.

Last edited by jtracking : 06-25-2007 at 12:22 PM. Reason: Edited for clarity.
Reply With Quote
  #3 (permalink)  
Old 06-25-2007, 12:41 PM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Re: Possible MySQL Doozie...

Hm, I even tried a join statement and that didn't work...

this is quite gruelling...

Code:
 
 select users.referer, contact_id, sales_id, product_no,contact_id from sales, salesdetail   
 from users, sales, salesdetail join salesdetail on sales.sales_id = salesdetail.sales_id
   and salesdetail.product_no = #pnumber#;
Reply With Quote
  #4 (permalink)  
Old 06-25-2007, 12:44 PM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Re: Possible MySQL Doozie...

Ok im going to try to select my info in a backwards motion, meaning im going to select the products from the salesdetail table where the product_no = #pnumber# and see if I can get the referal from that direction...

I'll be back...
Reply With Quote
  #5 (permalink)  
Old 06-25-2007, 01:13 PM
wige's Avatar
wige wige is offline
Moderator
WebProWorld Moderator
 

Join Date: Jun 2006
Location: United States
Posts: 1,648
wige RepRank 4wige RepRank 4wige RepRank 4
Default Re: Possible MySQL Doozie...

Just to restate what I think you are trying to accomplish: You want a list of all of the refferrers for all sales of a specific product by product ID?

Reading through the table, though, it looks like you have a 1-1 relationship between the users table and the contacts table, and it might be more effective to merge the tables. Then the sales table would have a reference to the user_id instead of the contact_id. As it stands now, I think you need to
Code:
SELECT user.referer FROM salesdetail LEFT JOIN sales ON salesdetail.sales_id=sales.sales_id LEFT JOIN contacts ON sales.contact_id=contacts.contact_id LEFT JOIN users ON contacts.contact_id=users.contact_id WHERE salesdetail.product_no = 'YOURPRODUCTNO' GROUP BY user.referer
Note that A. I have not tried this, and B. Group By will remove duplicates, so you won't see how many times that referrer resulted in a sale.
__________________
The best way to learn anything, is to question everything.
Interestingly Average Security Blog

Last edited by wige : 06-25-2007 at 01:16 PM.
Reply With Quote
  #6 (permalink)  
Old 06-25-2007, 01:20 PM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Re: Possible MySQL Doozie...

Well thank you Senior...I'm going to look into what you just posted...I'm rather confused with the left, outer, right and full joins...but I'm getting there...

I'll definitely keep you posted...
Reply With Quote
  #7 (permalink)  
Old 06-25-2007, 02:13 PM
computergenius computergenius is offline
WebProWorld Veteran
 

Join Date: Jul 2003
Location: Spain
Posts: 327
computergenius RepRank 1
Default Re: Possible MySQL Doozie...

Quote:
Originally Posted by jtracking View Post
The error states: Column 'sales_id' in field list is ambiguous
MySQL doesn't know which table you are referring to, because sales_id is obviously in more than one table.

You need to change it to SELECT thistable.sales_id - replacing thistable with the relevant tablename - you have done this in other places in your SQL.
__________________
Pete Clark
Sunny Southern Spain - http://hotcosta.com/Andalucia.Spain
Reply With Quote
  #8 (permalink)  
Old 06-25-2007, 02:21 PM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Re: Possible MySQL Doozie...

Yes I agree...

I've gotten a tad farther with my work and think I will be able to narrow down the referer by starting with the following queries;

<cfquery name="users" datasource="#request.dsn#">
select distinct sales_id from salesdetail where product_no = #pnumber#;
</cfquery>

<cfquery name="users2" datasource="#request.dsn#">
select contact_id from sales where sales.sales_id = #users.sales_id#;
</cfquery>

But for some reason when I try to output data from query users2 nothing is displaying but I'm SURE there's data there...

thanks again for your support CG, I'll get this surely in time...
Reply With Quote
  #9 (permalink)  
Old 06-25-2007, 02:22 PM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Re: Possible MySQL Doozie...

...shoot, i have to get the data from an array in the second query...
Reply With Quote
  #10 (permalink)  
Old 06-25-2007, 03:06 PM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Re: Possible MySQL Doozie...

Sweet. *sighs*

OK this wasn't so much of a doozie and I have to thank everyone for their assistance.

I got it to work and I did by going backwards instead of forwards, in other words,

instead of starting off by selecting the referer based on contact_id and sales_id stuff,

I first selected the product number from the salesdetail table,

then the sales_id from the same table,

then the contact_id from the sales table based on the contact_id in the salesdetail.sales_id

and then once I had that contact_id i fetched the referer from the users table...

the sql is all mixed in with coldfusion but if you need to see it just let me know and I will post it here...

Thanks everyone...
Reply With Quote
  #11 (permalink)  
Old 06-26-2007, 03:45 PM
jtracking jtracking is offline
WebProWorld Veteran
 

Join Date: Aug 2006
Location: Burlington, Ontario, Canada.
Posts: 406
jtracking RepRank 1
Default Re: Possible MySQL Doozie...

OK so I posted the resulting code and queries I used to make the script work here: Help me clean my ColdFusion Script.

I wanted to see if there were any Master CF developers who could help me clean my script up a little bit...
__________________
Post as-it-happens crime stories of criminal behaviour at crimedigg.com
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: doozie, mysql, possible



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
is php/mysql the only way to go? jilly eCommerce Discussion Forum 4 09-23-2006 01:50 PM
CSV to mySQL richkoi Database Discussion Forum 14 09-14-2006 03:42 PM
MySQL 5.0 is out TrafficProducer Database Discussion Forum 15 11-29-2005 10:12 AM
MySQL Bug!!! TrafficProducer Database Discussion Forum 3 11-26-2005 01:31 PM
PHP & MySQL webmasterjunkie Web Programming Discussion Forum 2 10-19-2004 06:41 PM


Search Engine Friendly URLs by vBSEO 3.0.0