TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 01-02-2008, 01:29 AM   #1 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 566
Thanks: 15
Village Idiot is on a distinguished road
Default Mysql problem, most likely related to left join

I am making a polling script, I have it able to get the top 3 items voted for. I am trying to left join the item data with the result data with this query
sql Code:
SELECT
    votes.poll_id AS votes_pollid,
    COUNT(item_id) AS count,
    item_id,
    items.*
FROM         
    votes
LEFT JOIN
    items
ON     
    votes.id = items.poll_id
WHERE     
    votes.poll_id = 1
GROUP BY
    item_id
ORDER BY
    count DESC
LIMIT     
    3;
(Thanks Wildhoney for the reformatting)
It tels me that votes.poll_id doesn't exist. That is simply not true, I can call votes.poll_id in a separate query and it comes up.
__________________
There are two ways to write bug-free code, only the third one works.

Last edited by Village Idiot : 01-02-2008 at 02:26 AM.
Village Idiot is offline  
Reply With Quote
Old 01-02-2008, 02:23 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,547
Thanks: 72
Wildhoney is on a distinguished road
Default

Hmmm. I don't see any immediate issues standing out. Could you possibly attach your MySQL dump with dummy data so we can emulate and ponder?

I reformatted the SQL if anybody else wishes to have a stab at it.

sql Code:
SELECT
    votes.poll_id AS votes_pollid,
    COUNT(item_id) AS count,
    item_id,
    items.*
FROM       
    votes
LEFT JOIN
    items
ON  
    votes.id = items.poll_id
WHERE  
    votes.poll_id = 1
GROUP BY
    item_id
ORDER BY
    count DESC
LIMIT  
    3;

Personal preference really on the formatting!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-02-2008, 02:28 AM   #3 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 566
Thanks: 15
Village Idiot is on a distinguished road
Default

Here is the dump, im running mysql 4.1.21
Code:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";



CREATE TABLE `ip` (
  `id` int(9) NOT NULL auto_increment,
  `ip` varchar(15) NOT NULL,
  `poll_id` int(9) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;



INSERT INTO `ip` VALUES(12, '76.217.61.249', 1);
INSERT INTO `ip` VALUES(11, '99.132.134.126', 1);


CREATE TABLE `items` (
  `id` int(9) NOT NULL auto_increment,
  `poll_id` int(9) NOT NULL,
  `name` varchar(255) NOT NULL,
  `image` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


INSERT INTO `items` VALUES(1, 1, 'item 1', 'images/klickers/krew18_franklin.jpg');
INSERT INTO `items` VALUES(2, 1, 'other', 'images/klickers/krew18_franklin.jpg');
INSERT INTO `items` VALUES(3, 1, 'pen4', 'images/klickers/krew18_franklin.jpg');
INSERT INTO `items` VALUES(4, 1, 'pen3', 'images/klickers/krew18_franklin.jpg');


CREATE TABLE `polls` (
  `id` int(9) NOT NULL auto_increment,
  `date` int(10) NOT NULL,
  `name` varchar(255) NOT NULL,
  `active` int(1) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


INSERT INTO `polls` VALUES(1, 112111111, 'Dev poll', 1);


CREATE TABLE `votes` (
  `id` int(9) NOT NULL auto_increment,
  `poll_id` int(9) NOT NULL,
  `item_id` int(9) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=43 ;

INSERT INTO `votes` VALUES(42, 1, 4);
INSERT INTO `votes` VALUES(41, 1, 3);
INSERT INTO `votes` VALUES(40, 1, 2);
INSERT INTO `votes` VALUES(39, 1, 3);
INSERT INTO `votes` VALUES(38, 1, 2);
INSERT INTO `votes` VALUES(37, 1, 1);
INSERT INTO `votes` VALUES(36, 1, 3);
INSERT INTO `votes` VALUES(35, 1, 2);
INSERT INTO `votes` VALUES(34, 1, 1);
__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is offline  
Reply With Quote
Old 01-02-2008, 01:12 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,547
Thanks: 72
Wildhoney is on a distinguished road
Default

Hmm. That works perfectly for me. Perhaps it's an issue in MySQL 4.1?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-02-2008, 03:43 PM   #5 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 566
Thanks: 15
Village Idiot is on a distinguished road
Default

Don't know, I've already given up and built it using multiple queries. It's a shame really, I have to use 3 more now. Il have to switch servers when I have the chance, but that causes hours of downtime.
__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is offline  
Reply With Quote
Old 01-03-2008, 03:39 AM   #6 (permalink)
The Wanderer
 
Join Date: Nov 2007
Posts: 9
Thanks: 0
macov is on a distinguished road
Default

Shouldn't be item_id removed from "GROUP BY" and "SELECT"?. When you try to COUNT item_id, you should list all columns (e.g., votes.poll_id votes_pollid, itmes.* in the GROUP BY (with an exception for item_id). At least it would work in Oracle...

Code:
SELECT
    votes.poll_id AS votes_pollid,
    COUNT(item_id) AS count,
    items.* 
FROM       
    votes 
LEFT JOIN 
    items
ON  
    votes.id = items.poll_id 
WHERE   
    votes.poll_id = 1 
GROUP BY 
    votes.poll_id,
    items.id,items.poll_id,items.name,items.image
ORDER BY 
    count DESC 
LIMIT   
    3;
macov is offline  
Reply With Quote
Old 01-04-2008, 06:09 AM   #7 (permalink)
The Gregarious
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 566
Thanks: 15
Village Idiot is on a distinguished road
Default

Took a little tweaking, but this works.

Code:
SELECT
    votes.poll_id AS votes_pollid,
    COUNT(item_id) AS count,
    items.* 
FROM       
    votes 
LEFT JOIN 
    items
ON  
    votes.item_id = items.id 
WHERE   
    votes.poll_id = 1 
GROUP BY 
    votes.item_id
ORDER BY 
    count DESC 
LIMIT   
    3;
Thanks!
__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is offline  
Reply With Quote
Reply



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

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


All times are GMT. The time now is 11:01 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0