hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : I need some help joining data from 3 MySQL tables, please
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

I need some help joining data from 3 MySQL tables, please

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 05-01-2010, 10:22 AM
SoftDux SoftDux is offline
Web Hosting Master
 
Join Date: Mar 2006
Location: Johannesburg,South Africa
Posts: 588

I need some help joining data from 3 MySQL tables, please


Hi all,



I am trying to display collective data from 3 MySQL tables:





The query I have, so far, is:



Code:
SELECT c . * , COUNT( m.id ) AS `members` 

FROM `jos_mls_teams` AS `c` 

LEFT JOIN `jos_mls_teams_members` AS `m` ON m.teamid = c.id

RIGHT JOIN ( SELECT u.name, u.lastvisitDate FROM `jos_users` AS `u` ) 

ON u.id = m.userid

GROUP BY c.id

LIMIT 0 , 30


But get the following error:



Quote:

#1248 - Every derived table must have its own alias


A google search results suggested the alias should be put after the bracket.



So, I change the code as follow, and move the AS `u` outside the right bracket:



Code:
SELECT c . * , COUNT( m.id ) AS `members` 

FROM `jos_mls_teams` AS `c` 

LEFT JOIN `jos_mls_teams_members` AS `m` ON m.teamid = c.id

RIGHT JOIN ( SELECT u.name, u.lastvisitDate FROM `jos_users` ) AS `u` 

ON u.id = m.userid

GROUP BY c.id

LIMIT 0 , 30


But then I get the error:



Quote:

#1054 - Unknown column 'u.name' in 'field list'






Basically, I need to display all the data from the "jos_mls_teams", total number of members linked to a user from the "jos_mls_teams_members" (basically counting all the rows where the corresponding user's id is in the userid field. Then I want to display that same corresponding user's name & email from another table.



Table structures to follow:



Code:
CREATE TABLE `jos_mls_teams` (

`id` int(11) NOT NULL auto_increment,

`userid` int(5) NOT NULL,

`memberid` int(11) NOT NULL,

`name` varchar(255) NOT NULL default '',

`email` text,

`area` text,

`arealeader` varchar(150) NOT NULL,

`founded` text,

`herder` varchar(100) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=19 ;







CREATE TABLE IF NOT EXISTS `jos_users` (

`id` int(11) NOT NULL auto_increment,

`name` varchar(255) NOT NULL default '',

`username` varchar(150) NOT NULL default '',

`email` varchar(100) NOT NULL default '',

`password` varchar(100) NOT NULL default '',

`usertype` varchar(25) NOT NULL default '',

`block` tinyint(4) NOT NULL default '0',

`sendEmail` tinyint(4) default '0',

`gid` tinyint(3) unsigned NOT NULL default '1',

`registerDate` datetime NOT NULL default '0000-00-00 00:00:00',

`lastvisitDate` datetime NOT NULL default '0000-00-00 00:00:00',

`activation` varchar(100) NOT NULL default '',

`params` text NOT NULL,

PRIMARY KEY (`id`),

KEY `usertype` (`usertype`),

KEY `idx_name` (`name`),

KEY `gid_block` (`gid`,`block`),

KEY `username` (`username`),

KEY `email` (`email`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=73 ;



CREATE TABLE IF NOT EXISTS `jos_mls_teams_members` (

`id` int(11) NOT NULL auto_increment,

`teamid` int(11) NOT NULL default '0',

`userid` int(11) NOT NULL default '0',

`leader` tinyint(1) NOT NULL default '0',

`sysid` int(11) NOT NULL,

`memberid` int(11) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=29 ;








P.S. just to be clear, I want to eventually remove the name & email fields from the `jos_mls_teams` table, since Joomla uses the `jos_users` table for registration purposes and it's easier to use the built-in registration than trying to reinvent the wheel

__________________
South African Web Hosting - http://www.SoftDux.co.za || SA WebHostingTalk - http://www.webhostingtalk.co.za

Reply With Quote


Sponsored Links
  #2  
Old 05-01-2010, 11:33 AM
stahightech stahightech is offline
Junior Guru Wannabe
 
Join Date: Oct 2009
Location: New York
Posts: 40
Hi,


This line seems to be the problem.

Code:
 RIGHT JOIN ( SELECT u.name, u.lastvisitDate FROM `jos_users` ) AS u
Since the derived table is aliased as "u", the table "jos_users", used in the subquery, does not have the alias anymore, so you need to remove it.

Code:
RIGHT JOIN ( SELECT id, name, lastvisitDate FROM `jos_users` ) AS `u`
Also, note that the column "id" needs to be added to the subquery, because it is used in the join

Code:
ON u.id = m.userid
Similar to the "id" column, if you want to display the "name" and "email" columns from the "jos_users" table, you will need to add them to your subquery

See the documentation on subqueries for more information.

http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

Hope this helps

Reply With Quote
  #3  
Old 05-01-2010, 12:16 PM
SoftDux SoftDux is offline
Web Hosting Master
 
Join Date: Mar 2006
Location: Johannesburg,South Africa
Posts: 588
Awesome!!!!

Thanx stahightech



Here's the (modified to show the name & lastvisitDate colums as well) query


Code:
SELECT c. * , COUNT( m.id ) AS `members`,u.name,u.lastvisitDate
FROM `jos_mls_teams` AS `c` 
LEFT JOIN `jos_mls_teams_members` AS `m` ON m.teamid = c.id
RIGHT JOIN ( SELECT id, name, lastvisitDate FROM `jos_users` ) AS `u` 
ON u.id = m.userid
GROUP BY c.id
LIMIT 0 , 30

__________________
South African Web Hosting - http://www.SoftDux.co.za || SA WebHostingTalk - http://www.webhostingtalk.co.za

Reply With Quote
Sponsored Links
Reply

Similar Threads
Thread Thread Starter Forum Replies Last Post
After joining 2 tables that have same columns, how to pick one from another? (MYSQL) grabmail Programming Discussion 10 03-31-2006 07:30 PM
MySQL Joining Multiple Queries sfjordan Programming Discussion 6 01-25-2006 02:43 AM
MySQL tables inside tables Lem0nHead Hosting Security and Technology 3 09-25-2004 09:05 PM
Moving data and tables between MySQL instances Diggler Programming Discussion 4 04-13-2004 09:53 AM
Joining tables from 2 databases in MySQL archangel777 Hosting Security and Technology 2 06-08-2002 12:00 AM

Related posts from TheWhir.com
Title Type Date Posted
Data Center Advisory Firm WiredRE Names CFO and Data Center Architect Web Hosting News 2012-01-17 10:17:02
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Stream Data Centers Names VP of Mission Critical Advisory Services Web Hosting News 2011-10-12 14:25:13
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43
Gazzang Data Encrytion Solution Adds Support for CloudLinux Platform Web Hosting News 2011-06-30 18:54:10


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?