Solved

MySQL - join 2 tables

Posted on 2011-03-04
3
515 Views
Last Modified: 2012-05-11
Hi,
I have 2 tables that I join with MySQL query below:

$query_mem_directory= "SELECT *
FROM `members_directory` AS members
JOIN category AS cat
WHERE cat.id = members.category
AND user_id = '9'"

$rs_directory = mysql_query($query_mem_directory, $conn_data) or die('error retrieving');
$row_rs_directory = mysql_fetch_assoc($rs_directory);

The results set produces 2 columns with name of "id"
How can I retrieve the value for the first id as $row_rs_directory['id'] will display value of the second id column?

id  user_id  updated  date_listed  date_expires  category  business_address  postal_address  busname  contact  phone  fax  mobile  email  website  blurb  more_details  image1  id  position  cat  subcat  min_order  cat_type  active  

Thanks in advance for your comments.


CREATE TABLE IF NOT EXISTS `category` (
  `id` int(11) NOT NULL auto_increment,
  `position` int(5) default '10',
  `cat` varchar(40) default 'Main',
  `subcat` varchar(50) NOT NULL,
  `min_order` tinyint(3) default NULL,
  `cat_type` varchar(4) default 'GN',
  `active` char(3) default 'Y',
  UNIQUE KEY `id_2` (`id`),
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=141 ;




CREATE TABLE IF NOT EXISTS `members_directory` (
  `id` int(11) NOT NULL auto_increment,
  `user_id` int(6) default NULL,
  `updated` varchar(3) default 'N',
  `date_listed` varchar(15) default NULL,
  `date_expires` varchar(15) default NULL,
  `category` varchar(255) default NULL,
  `business_address` varchar(255) default NULL,
  `postal_address` varchar(255) default NULL,
  `busname` varchar(255) default NULL,
  `contact` varchar(255) NOT NULL,
  `phone` varchar(255) default NULL,
  `fax` varchar(255) default NULL,
  `mobile` varchar(255) default NULL,
  `email` varchar(255) default NULL,
  `website` varchar(255) default NULL,
  `blurb` text,
  `more_details` text,
  `image1` varchar(60) default 'uploads/nopic.jpg',
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=819 ;
0
Comment
Question by:sabecs
3 Comments
 
LVL 3

Accepted Solution

by:
Duboux earned 500 total points
Comment Utility
Few things:

1)
Sometimes MySql rewrites the 2nd column with the same name to columname1: id1


2)
You could do the rewriting part yourself:
SELECT
members.id AS id_mem,
cat.id AS id_cat,
members.*,
cat.*
FROM `members_directory` AS members
JOIN category AS cat
WHERE cat.id = members.category
AND user_id = '9'


3)
To prevent these problems, don't use the same column names within the same database ;)
Use for instant mem_id instead of id ;)
0
 
LVL 22

Expert Comment

by:Thomasian
Comment Utility
It is recommended to specify the field names you will need instead of using "*" even if you will be needing all the fields. In case you have identical field names, you can assign aliases and use that to reference the field.

e.g. You can use CatID to reference the id field from category table in this query
$query_mem_directory= "SELECT members.id, members.position, cat.id `CatID`, cat.user_id
FROM `members_directory` AS members
JOIN category AS cat
WHERE cat.id = members.category
AND user_id = '9'"

Open in new window

0
 

Author Closing Comment

by:sabecs
Comment Utility
Thanks..
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now