Current location: Hot Scripts Forums » Programming Languages » PHP » Left Join with Where & Count is eluding me


Left Join with Where & Count is eluding me

Reply
  #1 (permalink)  
Old 12-09-05, 01:41 PM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Angry Left Join with Where & Count is eluding me

I'm trying to show some stats from my database and count the number of routes submitted, total length of routes etc. etc. I've got the query below displaying on my page fine.

However, this gives me a list showing userids. What I want to do is substitute the submittedby field in the jos_downloads_files table with the username in the jos_users table, this table has a URN called id.

My working Code
PHP Code:

$sql mysql_query("SELECT submittedby, SUM(routelengthmiles) as RoutesTotalLength, SUM(routeclimbfeet) as RoutesTotalClimb, COUNT(*) as HowMany

 FROM jos_downloads_files 
 GROUP BY submittedby 
 ORDER BY routelengthmiles DESC 
 LIMIT 3"
 
) or die (mysql_error()); 
 
while(
$row=mysql_fetch_array($sql)){ 
echo (
$row[submittedby]) .": " .($row[HowMany])." Routes, " number_format($row[RoutesTotalLength],0)." miles and " number_format($row[RoutesTotalClimb],0)." ft of ascent.<br />";


I know I need to use a (left?) join, but I've been trying to pull in the right columns and put the LEFT JOIN in the right place all afternoon and it's driving me bananas!

The linking fields are:
jos_users table: id
jos_downloads_files: submittedby

Could anyone please put me out of my misery?

Thanks!
Reply With Quote
  #2 (permalink)  
Old 12-12-05, 10:33 AM
arstanj's Avatar
arstanj arstanj is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by CutAndPaste
I'm trying to show some stats from my database and count the number of routes submitted, total length of routes etc. etc. I've got the query below displaying on my page fine.

However, this gives me a list showing userids. What I want to do is substitute the submittedby field in the jos_downloads_files table with the username in the jos_users table, this table has a URN called id.

My working Code
PHP Code:

$sql mysql_query("SELECT submittedby, SUM(routelengthmiles) as RoutesTotalLength, SUM(routeclimbfeet) as RoutesTotalClimb, COUNT(*) as HowMany

 FROM jos_downloads_files 
 GROUP BY submittedby 
 ORDER BY routelengthmiles DESC 
 LIMIT 3"
 
) or die (mysql_error()); 
 
while(
$row=mysql_fetch_array($sql)){ 
echo (
$row[submittedby]) .": " .($row[HowMany])." Routes, " number_format($row[RoutesTotalLength],0)." miles and " number_format($row[RoutesTotalClimb],0)." ft of ascent.<br />";


I know I need to use a (left?) join, but I've been trying to pull in the right columns and put the LEFT JOIN in the right place all afternoon and it's driving me bananas!

The linking fields are:
jos_users table: id
jos_downloads_files: submittedby

Could anyone please put me out of my misery?

Thanks!
PHP Code:

$sql "SELECT id, username, submittedby, SUM(routelengthmiles) as RoutesTotalLength, SUM(routeclimbfeet) as RoutesTotalClimb, COUNT(*) as HowMany

 FROM jos_downloads_files, jos_users 
 where jos_users.id=jos_downloads.submittedby
 GROUP BY submittedby 
 ORDER BY routelengthmiles DESC 
 LIMIT 3"

We suppose that you have in jos_users id and username fields. Now if you do: echo $row["username"]; you should get the username.
__________________
Happy coding!

----------------------------
http://www.jusupov.com - Computer Technology Blog
Reply With Quote
  #3 (permalink)  
Old 12-12-05, 11:50 AM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy

arstanj,

Thanks for your reply...

So now I've got:

PHP Code:

// Max Uploads by Distance

echo "<strong>Top 3 Uploaders By Distance</strong><br />";
$sql mysql_query("SELECT submittedby, SUM(routelengthmiles) as RoutesTotalLength, SUM(routeclimbfeet) as RoutesTotalClimb, COUNT(*) as HowMany where jos_users.id=jos_downloads_files.submittedby
 FROM jos_downloads_files, jos_users
 GROUP BY submittedby 
 ORDER BY RoutesTotalLength DESC 
 LIMIT 3"
 
) or die (mysql_error()); 
 
while(
$row=mysql_fetch_array($sql)){ 
echo (
$row[submittedby]) .": " number_format($row[RoutesTotalLength],0)." Miles<br />";

but I get an error:

PHP Code:

You have an error in your SQL syntaxCheck the manual that corresponds to your MySQL server version for the right syntax to use near 'where jos_users.id=jos_downloads_files.submittedby FROM jos_do 

I've also tried:

PHP Code:

$sql mysql_query("SELECT submittedby, SUM(routelengthmiles) as RoutesTotalLength, SUM(routeclimbfeet) as RoutesTotalClimb, COUNT(*) as HowMany where jos_users.id=jos_downloads_files.submittedby

 FROM jos_downloads_files, LEFT JOIN jos_users ON jos_downloads_files.submittedby=jos_users.id
 GROUP BY submittedby 
 ORDER BY RoutesTotalLength DESC 
 LIMIT 3" 
but still it won't work.

Have I done something wrong?
Reply With Quote
  #4 (permalink)  
Old 12-12-05, 12:50 PM
arstanj's Avatar
arstanj arstanj is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Can you post the Database structure of these two tables?
__________________
Happy coding!

----------------------------
http://www.jusupov.com - Computer Technology Blog
Reply With Quote
  #5 (permalink)  
Old 12-12-05, 12:53 PM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

CREATE TABLE `jos_users` (

  `
idint(11NOT NULL auto_increment,
  `
namevarchar(50NOT NULL default '',
  `
usernamevarchar(25NOT NULL default '',
  `
emailvarchar(100NOT NULL default '',
  `
passwordvarchar(100NOT NULL default '',
  `
usertypevarchar(25NOT NULL default '',
  `
blocktinyint(4NOT NULL default '0',
  `
sendEmailtinyint(4) default '0',
  `
gidtinyint(3unsigned NOT NULL default '1',
  `
registerDatedatetime NOT NULL default '0000-00-00 00:00:00',
  `
lastvisitDatedatetime NOT NULL default '0000-00-00 00:00:00',
  `
activationvarchar(100NOT NULL default '',
  `
paramstext NOT NULL,
  
PRIMARY KEY  (`id`),
  
KEY `usertype` (`usertype`),
  
KEY `idx_name` (`name`)
TYPE=MyISAM AUTO_INCREMENT=76 
and:
PHP Code:

-- Table structure for table `jos_downloads_files`

-- 

CREATE TABLE `jos_downloads_files` (
  `
idint(11NOT NULL auto_increment,
  `
sequenceint(11NOT NULL default '0',
  `
windowtitlevarchar(255NOT NULL default '',
  `
keywordsvarchar(255NOT NULL default '',
  `
realnamevarchar(255NOT NULL default '',
  `
islocaltinyint(3unsigned NOT NULL default '1',
  `
containeridsmallint(6NOT NULL default '0',
  `
useridint(11NOT NULL default '0',
  `
filepathvarchar(255NOT NULL default '',
  `
filesizevarchar(255NOT NULL default '',
  `
filetypevarchar(255NOT NULL default 'gpx',
  `
filetitlevarchar(255NOT NULL default '',
  `
descriptiontext NOT NULL,
  `
smalldesctext NOT NULL,
  `
autoshorttinyint(3unsigned NOT NULL default '1',
  `
licensetext NOT NULL,
  `
licenseagreetinyint(3unsigned NOT NULL default '0',
  `
priceint(11NOT NULL default '0',
  `
currencychar(3NOT NULL default '',
  `
downloadsint(11NOT NULL default '0',
  `
urlvarchar(255NOT NULL default '',
  `
iconvarchar(50NOT NULL default 'icon_gpx20x20.gif',
  `
publishedtinyint(3unsigned NOT NULL default '1',
  `
registeredtinyint(3unsigned NOT NULL default '2',
  `
useruploadtinyint(3unsigned NOT NULL default '3',
  `
download_texttext NOT NULL,
  `
recommendedtinyint(3unsigned NOT NULL default '0',
  `
recommend_texttext NOT NULL,
  `
featuredtinyint(4NOT NULL default '0',
  `
featured_st_datedate NOT NULL default '0000-00-00',
  `
featured_end_datedate NOT NULL default '0000-00-00',
  `
featured_prioritysmallint(6NOT NULL default '0',
  `
featured_seqsmallint(6NOT NULL default '0',
  `
featured_texttext NOT NULL,
  `
opsystemvarchar(50NOT NULL default '',
  `
legaltypevarchar(50NOT NULL default '',
  `
requirementstext NOT NULL,
  `
companyvarchar(255NOT NULL default '',
  `
releasedatedate NOT NULL default '0000-00-00',
  `
languagestext NOT NULL,
  `
company_URLvarchar(255NOT NULL default '',
  `
translatorvarchar(255NOT NULL default '',
  `
fileversionvarchar(50NOT NULL default '1',
  `
fileauthorvarchar(100NOT NULL default '',
  `
author_URLvarchar(255NOT NULL default '',
  `
filedatedate NOT NULL default '0000-00-00',
  `
filehomepagevarchar(255NOT NULL default '',
  `
screenurlvarchar(255NOT NULL default '',
  `
plaintexttinyint(3unsigned NOT NULL default '0',
  `
isblobtinyint(3unsigned NOT NULL default '0',
  `
chunkcountint(11NOT NULL default '0',
  `
groupidsmallint(6NOT NULL default '0',
  `
editgroupsmallint(6NOT NULL default '0',
  `
custom_1varchar(255NOT NULL default '',
  `
custom_2varchar(255NOT NULL default '',
  `
custom_3text NOT NULL,
  `
custom_4int(11NOT NULL default '0',
  `
submittedbymediumint(9NOT NULL default '0',
  `
submitdatedatetime NOT NULL default '0000-00-00 00:00:00',
  `
latitudestartvarchar(255NOT NULL default '0.000000',
  `
longitudestartvarchar(255NOT NULL default '0.000000',
  `
ukgridrefstartvarchar(10NOT NULL default '',
  `
latitudeendvarchar(255NOT NULL default '',
  `
longitudeendvarchar(255NOT NULL default '',
  `
ukgridrefendvarchar(10NOT NULL default '',
  `
routetypevarchar(15NOT NULL default 'Circular',
  `
routelengthmilesfloat NOT NULL default '0',
  `
routelengthkmtinyint(4NOT NULL default '0',
  `
routeclimbmetrestinyint(4NOT NULL default '0',
  `
routeclimbfeetint(11NOT NULL default '0',
  `
routeterrainvarchar(255NOT NULL default 'Road / Tarmac Paths / Gravel path, Open Ground',
  `
routestartinfovarchar(255NOT NULL default '',
  `
osexplorervarchar(255NOT NULL default '',
  `
oslandrangervarchar(255NOT NULL default '',
  
UNIQUE KEY `id` (`id`),
  
KEY `filetitle` (`filetitle`),
  
KEY `realname` (`realname`),
  
KEY `url` (`url`),
  
KEY `containerid` (`containerid`,`published`),
  
KEY `recommended` (`containerid`,`recommended`,`published`),
  
KEY `featured` (`containerid`,`featured`,`published`,`featured_st_date`,`featured_end_date`),
  
KEY `opsystem` (`containerid`,`opsystem`,`published`),
  
FULLTEXT KEY `words` (`filetitle`,`description`,`smalldesc`,`fileauthor`)
TYPE=MyISAM AUTO_INCREMENT=20 

Thanks!
Reply With Quote
  #6 (permalink)  
Old 12-12-05, 01:13 PM
arstanj's Avatar
arstanj arstanj is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,
now try this:
PHP Code:

$sql="SELECT jos_users.id, jos_users.username, submittedby, SUM( routelengthmiles ) AS RoutesTotalLength, SUM( routeclimbfeet ) AS RoutesTotalClimb, COUNT( * ) AS HowMany

FROM jos_downloads_files, jos_users
WHERE jos_users.id = jos_downloads_files.submittedby
GROUP BY submittedby
ORDER BY routelengthmiles DESC
LIMIT 3 "

__________________
Happy coding!

----------------------------
http://www.jusupov.com - Computer Technology Blog
Reply With Quote
  #7 (permalink)  
Old 12-13-05, 03:12 AM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

arstanj,

Here's the finished code that now works!

PHP Code:

// Max Uploads by Height

echo "<strong>Top 3 Uploaders By Height</strong><br />";
$sql mysql_query("SELECT jos_users.id, jos_users.username, submittedby, SUM(routelengthmiles) AS RoutesTotalLength, SUM(routeclimbfeet) AS RoutesTotalClimb, COUNT(*) AS HowMany
FROM jos_downloads_files, jos_users
WHERE jos_users.id = jos_downloads_files.submittedby
GROUP BY submittedby
ORDER BY routelengthmiles DESC
LIMIT 3"  
 
) or die (mysql_error()); 
 
while(
$row=mysql_fetch_array($sql)){ 
echo (
$row[username]) .": "number_format($row[RoutesTotalClimb],0)." ft of ascent.<br />";

HTH someone else.

Simon.
Reply With Quote
  #8 (permalink)  
Old 12-13-05, 03:36 AM
arstanj's Avatar
arstanj arstanj is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Question

Quote:
Originally Posted by CutAndPaste
arstanj,

HTH someone else.

Simon.
Hi,
What does it mean?
__________________
Happy coding!

----------------------------
http://www.jusupov.com - Computer Technology Blog
Reply With Quote
  #9 (permalink)  
Old 12-13-05, 03:46 AM
CutAndPaste's Avatar
CutAndPaste CutAndPaste is offline
Newbie Coder
 
Join Date: Mar 2005
Location: London, UK
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Hope This Helps..

I see loads of posts where people say "I fixed it.." or "I got it to work" with no further information and I always want to know what they did to get it to work or to fix it, so I try to remember to post details of the final working solution to any of my problems, so others can see what worked.

Cheers,

Simon

View results here
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wm-home.com No Ads , Join Webmaster Community & Get Free Domain extreme90 General Advertisements 0 09-17-05 03:06 AM
Join my forum and i will join yours! robertall General Advertisements 0 08-17-05 10:50 AM
Count Searches? fraggle PHP 7 09-19-04 01:59 PM
Post count, why? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 7 10-06-03 03:29 PM


All times are GMT -5. The time now is 06:22 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.